Latest Update: Add AdMob with Google Play Service
Related Post:
- Guide for Beginners: Monetize Your Apps with AdMob
- Add Admob Interstitial Ads in Android Apps
- Add Admob Smart Banners in Android
- Add Admob Ads in ListView
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
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.

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.
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"
Thank you very much, this is superb article…
AFAICT you’ve cvoered all the bases with this answer!
Whoa, thngis just got a whole lot easier.
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 🙂
It means you need to right click your project, and add the jar in “Build Path” configuration. Any questions, let me know.
[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?
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. 😛
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.
Hi Alan:
Your idea is great. I will try it and append your solution in the tutorial.
Thanks.
i get this error AdView missing required XML attribute “adSize” pls help…
add the “attrs.xml” file shown in the example will solve the problem
you need to add this file “attrs.xml”, in which you need to define the “adSize”
is it possible to convert in actionscript. I ‘d like to add in android app and I have adobe flash pro cs 5.5 and AIR and actionscript 3.0. 🙂
can I import-add only this xml file ?
https://developers.google.com/mobile-ads-sdk/docs/android/banner_xml?hl=el
Do not need to add the .jar file which I download it from adMob account?
Hi John,
If you want to put the Admob ads in your Flash/Flex application, you can check this Add Admob in Flash Android Air Application
hi James,
I work on adobe flash pro cs5.5 with actionscript I have an .fla file swf swc… The articles give the adMob extention only in xml files… have you an code in actionscript? I read this article
http://blog.dreamflashstudio.com/2011/02/admob-in-flash-with-adobe-air-for-android/
but must I have site?
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.
Hi John,
Please check this post, Make An Admob to Google Adsense Transition, I think it will affect your flash approach.
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?
Since Admob 4.3.1 released, if you work on Android SDK to r17 or later, you need to Admob SDK jar to “libs” folder. Here is the new post to guide how to add admob in your apps with SDK 4.3.1
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
Hi, Rupesh
I have updated the AdMob v6.0 Example.
Hi Jamas
I have to use admob this way only how can i solve the adSize error
Please help me out.
Hi, Thomas,
I have the latest Admob Tutorial with latest SDK.
Latest Update: Add AdMob v6.0 to Android Apps
Can you describe your problem in details and what’s your adSize error?
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.
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”
Thanks Thomas. Your help is great. I will update this post, so it will help other people.
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
you wanna show two ads bar at the same time?
android:configChanges=”keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize”/>
I am getting syntax here. String type not allowed
First, what is the Android SDK version and what’s your AdMob sdk versin?
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?
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.
java.lang.NullPointerException
Hello from Ukraine
I have a problem
Pleace help me if you can.
java.lang.NullPointerException
Hello from Ukraine
I have a problem
Pleace help me
Great article… very helpful.
Thanks a mil
Thanks a lot its working
What if you are getting the same error with Adobe Flash CS5???
You can check this: Add Admob Ads in Flash Based Android Apps
excellent! you helped me out!!