Ankur Walia
Ankur Walia

Reputation: 23

placing admob ad at the bootom of webview

i want some help for my app i am using webview(only one single webview in one xml file) and using local html pages in it i have placed admob banner ads using following java code

      private AdView adView;
      adView = new AdView(this, AdSize.BANNER, "my publisher id");
      WebView layout = (WebView) findViewById(R.id.webView1);
       layout.addView(adView);
      adView.loadAd(new AdRequest());

my problem is that on starting the app ad is displayed on top of my app, thus hiding some content on my page and when user scrolls down he is not able to see the ad. how can i get the ad to show at bottom such that it is always visible to user?

      <?xml version="1.0" encoding="utf-8"?>

     <WebView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/webView1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      />

and i have set this webview in my acctivity

    setContentView(R.layout.webview);

Upvotes: 2

Views: 9327

Answers (2)

devsathish
devsathish

Reputation: 2409

Here is the recent layout XML that supports webview and banner ad at the bottom of the page. Here is the code that works for me:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"></com.google.android.gms.ads.AdView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/adView"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</RelativeLayout>

Change the @string/banner_ad_unit_id appropriately.

webView1 is the id of the webview.

If you're layout throws AdView not found exception, make sure you add play-services-ads dependency. You've to add/update it in your app's build.gradle file as follows:

dependencies {
    compile 'com.google.android.gms:play-services-ads:7.8.0'
}

This is how I initiate ad request:

AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

Upvotes: 1

sampullman
sampullman

Reputation: 176

Because a WebView is an AbsoluteLayout, you can position its child views like so:

    AbsoluteLayout.LayoutParams params = AbsoluteLayout.LayoutParams(width,height,X-position,Y-position);
    adView.setLayoutParams(params);

However, it would be easier to put the WebView in a LinearLayout, so when you add the ad it will automatically appear at the bottom. For example:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <WebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

Your code would have to change to look something like this:

    private AdView adView;
    adView = new AdView(this, AdSize.BANNER, "my publisher id");
    LinearLayout root = (LinearLayout)findViewById(R.id.main);
    root.addView(adView);
    adView.loadAd(new AdRequest());

Another option is including the ad in the xml. You can find out how to do that in the AdMob https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#androiddeveloper guide. Taken from the site:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
      <!-- WebView goes here -->
      <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>
      </LinearLayout>

Upvotes: 5

Related Questions