2013年7月14日日曜日

Add Google Admob in Android Application


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.

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.
01<?xml version="1.0" encoding="utf-8"?>
02<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03      package="com.jms.AdmobExample"
04      android:versionCode="1"
05      android:versionName="1.0">
06    <uses-sdk android:minSdkVersion="8"/>
07    <application android:icon="@drawable/icon"android:label="@string/app_name">
08        <activity android:name=".AdmobExample"
09                  android:label="@string/app_name">
10            <intent-filter>
11                <action android:name="android.intent.action.MAIN" />
12                <category android:name="android.intent.category.LAUNCHER" />
13            </intent-filter>
14        </activity>
15        <activity android:name="com.google.ads.AdActivity"
16                      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
17    </application>
18
19  <uses-permission android:name="android.permission.INTERNET"/>
20  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
21</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:
01<?xml version="1.0" encoding="utf-8"?>
02<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03    android:orientation="vertical"
04    android:layout_width="fill_parent"
05    android:layout_height="fill_parent"
06    >
07
08<imageView
09    android:layout_width="wrap_content"
10    android:layout_height="wrap_content"
11    android:src="@drawable/a"
12    />
13
14<com.google.ads.AdView
15    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
16    android:id="@+id/adView"
17    android:layout_width="fill_parent"
18    android:layout_height="fill_parent"
19    ads:adSize="BANNER"
20    ads:adUnitId="Your Admob ID"
21/>
22</linearLayout>
3. Send AdMob AdRequest in your Activity class:
1AdView adview = (AdView)findViewById(R.id.adView);
2AdRequest re = new AdRequest();
3re.setTesting(true);
4adview.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
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:
1xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"


Try example the  result, download AdmobSample apk