Latest Update: Add AdMob with Google Play Service

Related Post:

Old Post About AdMob:
Add AdMob v6.0 to Android Apps
Add Admob To Your Android Apps With SDK 4.3.1

Admob is a new way to monetize mobile applications. Currently, it supports multi-platform, including iPhone, Android, WebOS, and Flash Lite. In this AdMob for Android example, I will show you how to integrate Admob in your android applications defined in layout XML file.

Try Example: download and run AdMobExample.apk on your android phone
chart

First of all, we need to go to the Admob website to register an account, in order to get the Admod account ID. Then, let’s go the google code to download the latest Google AdMob Ads SDK for android. You also can get download it from your admob “Sites & Apps” setting page. After we get the AdMob SDK, we need to add the AdMob sdk jar in our android project. For those who want to implement the AdMob in Android Java code, you can copy the Jar file in the project and include it in your android project Build path. For more details, please refer the post Add AdMob v6.0 to Android Apps. If you want to layout the AdView in layout xml file, you need to add jar file in the /libs/ folder under your android project.

AdMob Build Path
AdMob Build Path

1. For Admob, it’s better to add the com.google.ads.AdActivity in your Manifest.xml file. And don’t forget to add the user permission for it. Below is my mainfest.xml example.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jms.AdmobExample"
      android:versionCode="1"
      android:versionName="1.0">
	<uses-sdk android:minSdkVersion="8"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AdmobExample"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
		<activity android:name="com.google.ads.AdActivity"
		              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

2. The basic class in android applications are composed of View objects. For Admob, we need use AdView class which is provided in the SDK jar file. Actually, it will be put into the main layout file, it’s easy for you to build your own layout. Let’s see my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<imageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/a"
    />

<com.google.ads.AdView
  	xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
	android:id="@+id/adView"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	ads:adSize="BANNER"
  	ads:adUnitId="Your Admob ID"
/>
</linearLayout>

3. Send AdMob AdRequest in your Activity class:

        AdView adview = (AdView)findViewById(R.id.adView);
        AdRequest re = new AdRequest();
        re.setTesting(true);
        adview.loadAd(re);

There are a most important things you need to know:

  • At the first time, the Admob Ads will take 1 or 2 minutes to show.
  • In the simulator, I never try it successfully (As Alan Jay Weiner‘s solution, it maybe because I have two INCs, internet adapter, in my computer. His solution is that disable all other NICs in computer, only leave one INC working which can access the internet, and restart the emulator. Then, it will work properly. Thanks Alan. ). I always get the time out error. I only try it in my HTC Desire, and it really works.
  • Some times, it will show no ads to show. It’s not the problem, because it really doesn’t have ads to show for some situations.

Compile Errors Solutions

One common compiling error is “Error inflating class com.google.ads.AdView”. The error message is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jms/com.jms.InfiniteGalleryActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class com.google.ads.AdView

This is one of the compile errors which many android programmers meet with. The solution is creating the /libs folder under your android project root folder and putting the AdMob Jar file inside.

AdMob Build Path
AdMob Build Path

Another compiling error is “Required XML attribute missing”. The error message is:

Could not initialize AdView: Required XML attribute “adSize” missing

Thanks Thomas giving me the suggestion. This is because the Android can not find the AdSize defination. It can be solved by specifying the right ads name spacing in the layout xml:

xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
Previous PostNext Post

39 Comments

  1. Sorry for what might seem like a very simple question but I am a total novice on this. I have an android app built in flash cs5 which I have yet to publish but it’s pretty much ready to go. Want to add adverts in but I’m not sure what you mean by “android application Build Path”. I’m not a developer so some of this language is totally new to me but I’m trying to figure it out. Any help would be great 🙂

    1. It means you need to right click your project, and add the jar in “Build Path” configuration. Any questions, let me know.

  2. [2011-08-20 22:40:54 – TrickshotLite] D:\EclipProj\TrickshotLite\res\layout\main.xml:12: error: No resource identifier found for attribute ‘adSize’ in package ‘com.jms.AdmobExample’
    [2011-08-20 22:40:54 – TrickshotLite] D:\EclipProj\TrickshotLite\res\layout\main.xml:12: error: No resource identifier found for attribute ‘adUnitId’ in package ‘com.jms.AdmobExample’

    I am getting these errors. I did not have an attrs.xml file so I created one. Any Ideas?

    1. Yes, you need to create this “attrs.xml” file. As the example says, the full path is “res/values/attrs.xml”. Create it manually, then copy and paste the example code in it.

      Let me know if u have any other problem. 😛

  3. James,
    About your not getting ads in the emulator – you should be able to (I do…).

    Do you have more than one NIC in the machine on which you’re running the emulator?

    I do, and using Wireshark could see that the emulator was trying to get to the Internet by the wrong NIC.

    I disabled that NIC, reloaded the emulator and it worked properly. Annoying to have to play that dance – disable the NIC for the emulator, re-enable it when I want to use that network – but it works. Once the emulator is running, you can re-enable the other NIC until you close and restart the emulator.

        1. Yes. If you use StageWebView to load the admob ad, you must need a server. You can find some free hosting to host your admob ad page.

  4. Hello,
    I get these errors in my java activity file:
    Adview can not be resolved to a type
    Adrequest can not be resolved to a type
    Adview can not be resolved to a variable

    Do you know what I have done wrong?

  5. Hi tried your code but my code stuck at line setContentView(R.layout.main);
    i add attrs.xml but he cannot find adSize.showing adSize missing.error – android.view.InflateException. Error inflating class

      1. i have to add ads at bottom of the screen and top of all the widgets.In your latest example m unable to do this thats why i have to use this way.

  6. i solved “AdView missing required XML attribute “adSize”” problem by replacing xmlns:ads=”http://schemas.android.com/apk/res/com.jms.AdmobExample” with xmlns:ads=”http://schemas.android.com/apk/res/com.google.ads”

  7. hello i have made this admob example but i can see only one add on device i want more add randomly change can u have solution or code pls suggest me
    thanks in advance

  8. android:configChanges=”keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize”/>

    I am getting syntax here. String type not allowed

  9. Hey thanks for the article,

    I’m having a problem. The app doesn’t show any ads. I see “Above is the Ad Banner which is returned by Admob!” but there is no ad. I’ve left it for a long time. I have reinstalled the apk.

    In my Admob dashboard my site status is still red.

    -I am testing on my Galaxy Note with Android 4.1.1
    -The AdmobExample is set to 4.0.3
    -I used your AdmobExample and changed my Publisher ID

    I am going to create an emulator to try test.

    (I also downloaded your admobexample apk here http://www.jmsliu.com/example/androidadmobtest/AdmobV6Example.apk) and I also see no ads there.

    Do you have any suggestions?

    1. Hi, Darren,

      I am using samsung galaxy tablet and my htc desire to test the example. Both of my device can get the ads. I think it also depends on your location. If there is no related ads in your region, the admob may not return any ads. This is what I guess.

      If you have any questions, let me know.

Leave a Reply to James Cancel reply

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