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
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015