[go: nahoru, domu]

The Google AdMob SDK makes it easy to serve AdMob ads and monetize your mobile applications. If your app contains a ScrollView, the placement of the ad in your layout can be tricky. In this post, we will discuss the best practice for adding AdMob ads to scrollable content.

Consider the following XML snippet (XML attributes omitted for brevity):

<ScrollView>
   <LinearLayout>
    <TextView android:text="Place very long string here..."/>
      </LinearLayout>
</ScrollView>

Let’s say we want to place an ad in this layout. Defining an AdView in XML is pretty straightforward with the Google AdMob SDK:

<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"
    ads:adSize="BANNER"
    ads:testDevices="TEST_EMULATOR"
    ads:loadAdOnCreate="true"/>

Now we have to determine where to insert the AdView in the layout. If we were to insert the AdView inside the LinearLayout after the TextView, the ad would be displayed at the bottom of the ScrollView.


This is a poor ad placement from the user's perspective. Depending on the length of the app content, the ad either sits awkwardly somewhere in the middle of the screen or is hidden at the bottom of the ScrollView.

To improve the user experience, we can move the ad outside the ScrollView and use a RelativeLayout to pin the ad to the bottom of the screen. This is the needed XML markup:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollLayout"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_above="@+id/adView">
        <LinearLayout android:layout_height="fill_parent"
            android:layout_width="fill_parent">
            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Place very long string here..."/>
        </LinearLayout>
    </ScrollView>
    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"
        ads:adSize="BANNER"
        ads:testDevices="TEST_EMULATOR"
        ads:loadAdOnCreate="true"/>
</RelativeLayout>

The layout_alignParentBottom attribute pins the AdView to the bottom of the screen and the android:layout_above attribute sets the ScrollView above the AdView. If the ScrollView is not explicitly set to sit above the AdView, the ad will overlay the bottom of the ScrollView. Let’s see what the final result looks like.


This layout looks much cleaner! The ad is displayed at the bottom of the screen and will stay locked to the bottom as we scroll through the app content, providing a more natural user experience.

If you have questions about this post or about the AdMob SDK, feel free to ask us in the forum or join us at our upcoming office hours Hangout session. Start monetizing your mobile applications today!


We’ve seen a lot of forum activity about how to migrate from v4.1.1 to v4.3.1 of the Google AdMob Ads SDK for Android, so we’d like to share three easy steps to get you up and running with the latest version.

  1. The first step to migrating is downloading the AdMob SDK and adding it to your build path.

  2. You will need to have an Android SDK of 3.2 or higher. Once you have a compatible version, set target=android-13 or higher in your project.properties file of your application to compile against the version you just downloaded.

    Don’t worry, you can still build your apps on all Android devices down to Android 1.5! Just set <uses-sdk android:minSdkVersion="3"/> in your AndroidManifest.xml file. You can read more about the uses-sdk element to help you configure the appropriate devices for your app.


  3. Finally, replace the old com.google.ads.AdActivity element in your manifest with this new definition:
    <activity android:name="com.google.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

That’s it! You should now be able to run your application using the latest SDK without any errors. Please post to our forum if you have any questions about the migration.