WebView and AdView with SMART_BANNER without xml

204 views
Skip to first unread message

NB

unread,
Jun 6, 2014, 5:16:59 PM6/6/14
to google-adm...@googlegroups.com
Hi,

I use the Google Play Services SDK with SMART_BANNER and a webview.

Therefore I created the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             
xmlns:ads="http://schemas.android.com/apk/res-auto"
 
android:id="@+id/rl"
             
android:orientation="vertical"
             
android:layout_width="fill_parent"
             
android:layout_height="fill_parent">
<WebView
   
android:id="@+id/webView1"
   
android:layout_above="@+id/adView"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent" />


<com.google.android.gms.ads.AdView android:id="@+id/adView"
                         
android:layout_width="fill_parent"
                         
android:layout_height="wrap_content"
                         
android:layout_alignParentBottom="true"
 
ads:adUnitId="ca-app-pub-0/0"
                         
ads:adSize="SMART_BANNER"/>


</RelativeLayout>


In the java I used this code:

AdView adView = (AdView)this.findViewById(R.id.adView);
//adView.setAdSize(AdSize.SMART_BANNER);
//adView.setAdUnitId(idbanner);
adView
.loadAd(new AdRequest.Builder().build());

I would like to insert the AdUnitId dynamically. But I can't use lines 2 and 3 in the second code because with xml I must provide these information in the xml.

I tried it without defining AdView in xml, only in the code. But then the banner is over the webview. I also tried gravity...

Could you please give me a short help or a code snipped?

Thanks very much!

Eric Leichtenschlag

unread,
Jun 13, 2014, 8:22:34 PM6/13/14
to google-adm...@googlegroups.com
Instead of the AdView, you can define a FrameLayout in the same space and have the WebView sit above the FrameLayout:

<FrameLayout android:id="@+id/adViewFrame"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:layout_alignParentBottom="true"/>




Then in code, you can create the AdView from scratch and add it into your FrameLayout: 

AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(idbanner);
FrameLayout layout = (FrameLayout) this.findViewById(R.id.adViewFrame);
layout.addView(adView);
adView.loadAd(new AdRequest.Builder().build());

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages