AdMob Ad Incorporated into an Android Activity

A Tutorial on Adding an AdMob Ad to an Android App

Update: The latest version of this article is now on TekEye.UK

Displaying advertisements (ads) is one way of monetizing an App. To monetize (monetise) something is turn turn it into cash. That term applied to Apps and web pages refers to getting revenue through ads or paid content. Showing ads is one way of generating an income from the time and effort spent developing the App. Though a reasonably successful App is required to generate worthwhile funds. Other techniques include charging a price for the App and providing additional content within the App for a fee (In-App Billing).

Google makes it relatively easy to include ads into an App as the simple example code here shows. This tutorial should help those who may have found it difficult to get the example provided with the online AdMob documentation to work. It is assumed that your computer is configured for developing Apps using the Eclipse Integrated Development Environment (IDE) (see Set Up Windows for Android Development if not), and that you can create and run a simple App (see Your First Android Java Program if in doubt). All the steps required to get ad serving in your App working are covered. So read on to find out how to add AdMob to Android Apps.

Get an AdMob Account

You get a very small payment for each ad that is selected (clicked or pressed on) from a device that is running your App. So that Google can make that small payment it must identify in which App the ad was selected. This is done by placing a unique Publisher Id in the code that is showing the ads. To get the unique id for each App you need an AdMob account. Head over to and sign up, you will need a Google account to register.

Target SDK Must be 3.2 or Higher

Your App is ready for publishing, lets put in the ad serving code so that when you activate it on Google Play it can (potentially) make you some money. In this tutorial we will add the code to an existing example project, in this case the code that was written for the article Two Line Lists In Android. You can also follow this tutorial but use your own App to add the code.

Download the file (it is also available from the Example Android Projects page). Create a new Android Project in Eclipse using the File menu, select New then Android Project. Fill in the Project Name, here State Capitals is used. Click Next and select a build target from the installed APIs. Use the latest API available and, according to the documentation, it must be at least Android 3.2 (API Level 13). Here Android 4.0.3 (API Level 15) was selected, the latest available at the time of writing. Click Next and enter the Package Name, here it is biz.tekeye.statecapitals, leave Create Activity checked with the default Activity name, click Finish.

With the State Capitals project highlighted in Package Explorer use the File menu and select Import. Select Archive File under the General folder and click Next. Browse to and select the file. Uncheck the instructions.txt file and select Finish. If a message asking to update Launch Configurations appears the package name was not entered correctly, delete the project and create and import again. Open the AndroidManifest.xml file and under Uses Sdk ensure that Target SDK version is set to 15 (or at least 13). Use the SDK Manager to update to a later API level if it is not installed (see Keeping the Android SDK Updated). When copying from the code blocks see the tips in Copying Code from the Articles:

[code lang=”xml”]<uses-sdk android:minSdkVersion=”3″ android:targetSdkVersion=”15″/>[/code]

The State Capitals App should now run on a connected device or emulator image.

Add the AdMob Library to the Project

Use the SDK Manager to install the Google AdMob Ads SDK. It will be towards the bottom of the list of Packages under Extras.

Android Admob SDK is Installed with SDK Manager

With the project highlighted in Package Explorer open the project Properties via the File menu then Properties. Click on Java Build Path in the left hand list and then the Libraries tab. Select Add External Jars and browse to the Google AdMobs Ads SDK  jar file. It is likely to be under the default Android SDK extras folder unless the Android SDK or AdMobs SDK was installed elsewhere. In Windows this is usually C:\Program Files\Android\android-sdk\extras\google\admob_ads_sdk. Select GoogleAdMobAdsSdk-6.0.1.jar, or whatever is the latest version, and close the file selection dialog. Click on the Order and Export tab and ensure that the Google AdMobs Ads SDK  jar file is checked for export:

Google AdMobs SDK Jar File Marked for Export

Select OK to close the dialog.

Reference the Ad Serving Activity in the Manifest

A separate Activity controls ad sourcing and serving. It needs to be added to the AndroidManifest.xml file for the project, add this code just before the closing application tag:

[code lang=”xml”]<activity android:name=”” android:configChanges=”keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize”/>[/code]

Add Permissions

AdMob Ads are served over the Internet connection. Thus Internet and Network State permissions need adding to the manifest, add these before the application element:

[code lang=”xml”]<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>[/code]

The full AndroidManifest.xml should look like this:

[code lang=”xml”]<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
android:versionName=”1.0″ >
<uses-sdk android:minSdkVersion=”3″ android:targetSdkVersion=”15″/>
<uses-permission android:name=”android.permission.INTERNET”/>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>
<application android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” >
<activity android:name=”.StateCapitalsActivity”
android:label=”@string/app_name” >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
<activity android:name=””

Get the App’s Publisher Id

Add a new Site or App in AdMobBefore we add the code to the App which will serve the ads we need to grab a Publisher Id. The Id will be embedded into the ad serving code. Log into your AdMob account and from Sites & Apps chose Add Site/App. This brings up some choices: Android App, iPad App, iPhone App and Windows Phone 7 App. Obviously here we select the Android App option. This reveals a Details form to fill in:

App Name

What you want the App to be called, in our example it is State Capitals. If using your own App enter its name. Hopefully you have been creative with the name, to make it stand out, yet be relevant when someone does a search.

Android Package URL

This is the web location to obtain the Apps APK. For an App published on Google Play it is the address of the details page for the App in the format market://details?id=<packagename> where packagename is the underlying identifier for the App. This is usually the namespace of the code package that contains the first loaded activity. For the State Capitals example we used biz.tekeye.statecapitals. So the full URL would be market://details?id=biz.tekeye.statecapitals. For you own App you will use its package identifier. If unsure what the package identifier is open the AndroidManifest.xml file. Then look at the value of the package attribute on the root manifest element. The URL can be a normal web site page address if the App is not to be hosted on Google Play.


Select the Google Play Category for the App, there are about twenty, for State Capitals we can choose Reference.

App Description

Enter a description for the App.

When the details are completed enter the Captcha words and press Continue. Back on the Sites & Apps tab your App will be listed, move the mouse over the listing and you will see a Manage Settings button.

Mouse Over App Listing for Manage Settings Button

AdMob App DetailsSelect Manage Settings to bring up the State Capitals AdMob information, the Publisher Id will be visible. Make a note of the ID, e.g. copy and paste it into a text file or a comment in your code. Each App will have its own Publisher Id, so that Google, and you, can track how successful the ads served have been on an App by App basis.

Add the Ad Serving Code

Back in Eclipse open the main.xml layout file under the res\layout folder. Immediately before the ListView element add the following code:

[code lang=”xml”]< android:id=”@+id/adView”

In the ads:adsUnitId attribute set it to the value obtained from the AdMobs App details page. There are several ad sizes available, as discussed in our AdMob Ad Sizes article. Fortunately you can leave the AdMob SDK to choose the appropriate size by setting the ads:adsize attribute to SMART_BANNER (see Issues towards the bottom of the article on when not to use SMART_BANNER).

Also add the ads namespace to the root LinearLayout element:

[code lang=”xml”]xmlns:ads=””[/code]

For our simple State Capitals App the full layout should look like this:

[code lang=”xml”]<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=””
android:orientation=”vertical” >
< android:id=”@+id/adView”
<ListView android:layout_height=”wrap_content”
<TextView android:layout_width=”fill_parent”
android:text=”@string/hello” />

The program should now execute.

Screen Showing AdMob Test Ad

Serve Test Ads to Your Devices

When you are testing your App it does not make sense that Google resources are wasted on sending ads to your test devices. To receive test ads instead of real ads replace the TEST_DEVICE_ID string in the ads:testDevices attribute with the value unique to your device. To get that value look in LogCat for a Ads tag entry with text similar to “To get test ads on this device, call adRequest.addTestDevice(“0FXX…XX4F”);”.

To open the LogCat window when not debugging (if not already open) use the Window menu in Eclipse, select Show View then Other, Android and LogCat. If necessary set the search filter at the top of LogCat to tag:ads. The value to replace TEST_DEVICE_ID is the long hexadecimal number string between the quotes.



When you add the AdMob code to an App and run it you may see an error message saying that the App has stopped unexpectedly. If the App was working prior to the ad code being incorporated you will likely find a run time exception in LogCat as the cause of the error. Looking at the error messages in the detail you may find Error inflating class and Caused by: java.lang.ClassNotFoundException: in loader dalvik.system.PathClassLoader. This points towards the AdMob code not being present in the App. Go back and check, under the Java Build Path option, on the project Properties that the Google AdMobs Ads SDK jar file is checked for export. This gets the missing code included in the APK file.

LogCat Errors

You may see this message in LogCat if running an App with ads on Android 1.5.

“Could not find class ‘android.gesture.GestureStore’, referenced from method
VFY: unable to resolve new-instance 33 (Landroid/gesture/GestureStore;) in Lcom/google/ads/internal/j;”

When testing on early APIs in the emulator and swiitching from portrait to landscape and back to portrait LogCat may report a error:

“An error occurred while loading data in AdWebView:

These errors do not appear to affect operation of the App.

QVGA Screens

BANNER ads will not show on QVGA Portrait screens and may prevent the screen from displaying or cause the App to disappear from memory. SMART_BANNER ads will not work correctly in Android 1.5 QVGA Portrait and Landscape screens and in QVGA Landscape screens on other versions of Android. If your App supports QVGA screens it is suggested that BANNER is used and that the screen width is tested before enabling ad serving. Set ads:adSize=”BANNER” and ads:loadAdOnCreate=”false”, then add this code to onCreate:

[code lang=”Java”]AdView adView = (AdView)findViewById(;
adView.loadAd(new AdRequest());[/code]

And override onDestroy:

[code lang=”Java”]@Override
public void onDestroy() {
if (adView != null)


To incorporate AdMob ads into an Android App the following steps need completing:

  1. Get an AdMob Account
  2. Target your App at API level 13 or higher (Android 3.2 or higher, it can still run on older versions).
  3. Use SDK Manager to download the AdMob SDK
  4. Reference the AdMob SDK jar file in the Java Build Path (do not forget to tick the GoogleAdMobAdsSdk jar file in the Order and Export tab).
  5. Add the Ad Serving Activity to the manifest file.
  6. Add the INTERNET and ACCESS_NETWORK_STATE permissions to the manifest file.
  7. Get a Publisher Id for the App from your the AdMob account pages.
  8. Add the ad serving code and put the Publisher Id into it (do not forget the namespace if using layout code).
  9. Put your device id in the testDevices attribute.
  10. If you support QVGA screens do not enable ads in portrait orientation and user BANNER ads.
  11. Test! Test! Test your App on various API levels.

2 thoughts on “AdMob Ad Incorporated into an Android Activity

  1. I am a bit confused on How do I get Publisher Id before I post it on google play. I have my app completed , but havent posted it anywhere on the internet. I want to post it only on google play but with ads from AD mob.
    SO how do I get publisher id before uploading it on google play.
    Its more like a chicken and the egg problem.
    Am I missing something here ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.