Ankur Walia Ankur Walia - 4 months ago 135
Android Question

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);

Answer

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>