Eric Bergman-Terrell's Blog

Android Programming Tip: Displaying AdMob Ads
May 14, 2011

My new Android application, Vault 3 for Android, is advertisement-supported. The ad provider is AdMob. This blog post describes the code I wrote to integrate AdMob advertisements with my application.

I added a <LinearLayout> with an id of "adLayout" for the banner ad in Vault 3's main activity (see below). The main activity's onCreate method calls my addAds method, which creates an AdView and attaches it to the <LinearLayout>. Then it arranges for the ad to be updated every minute by instantiating and running the updateAdTask. The updateAdTask changes the ad, and then arranges to run again in 60 minutes (60,000 milliseconds).

Vault3.java:

package com.ericbt.Vault3;

...

public class Vault3 extends TabActivity {
private Handler adTimerHandler = new Handler();
private AdView adView;
...
private Runnable updateAdTask;

@Override
    public void onCreate(Bundle savedInstanceState) {
...
        setContentView(R.layout.main);
        
...
        
    addAds();
...
}

private void addAds() {
// Create the adView
    adView = new AdView(this, AdSize.BANNER, {adUnitId});

    LinearLayout layout = (LinearLayout)findViewById(R.id.adLayout);

    layout.addView(adView);
    
    updateAdTask = new Runnable() {
@Override
public void run() {
changeAd();

// Request a new ad every 60 seconds.
adTimerHandler.postDelayed(this, 1000 * 60);
}
};

updateAdTask.run();
}

public void changeAd() {
...

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/adLayout"
    >
    </LinearLayout>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        >
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"
        >
        </FrameLayout>
    </LinearLayout>
</TabHost>

Postscript: My experience with AdMob was disappointing. The ads simply didn't perform as well as I had hoped. I ended up replacing AdMob ads with a large "upgrade" button in the free version of Vault 3 for Android.

Keywords: Android, AdView, Java, AdMob, Advertisements

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021