Jimmy’s Blog – Adding AdMob’s ads to Android Application
by jimmy on Jul.04, 2012, under English, Java, Programming, Uncategorized
[Switch to Mobile Edition]Finally I got some spare time to make new article on my blog
One of the easy way to monetize our Android application is by adding ads.
This tutorial will give basic explanation about how to add AdMob ads to Android Application.
Prerequisite:
- AdMob SDK (https://developers.google.com/mobile-ads-sdk/download)
- Your favorites IDE (I use Eclipse for this tutorial) for IDE
Create Android Project
I create a simple android project with 1 activity and 1 layout.
AdMob library
Copy AdMob lib file (GoogleAdMobAdsSdk-6.0.1.jar) to your lib folder, so Eclipse will automatically include it to your build.
And also add the library to project’s build path.
Permissions and AdMob Activity
There’s 2 things that we need to add to ‘AndroidManifest.xml’
- Permissions for ‘android.permission.INTERNET’ and ‘android.permission.ACCESS_NETWORK_STATE’
- AdMob ‘com.google.ads.AdActivity’ Activity for handling configuration changes.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="gnu.jimmod.android" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" 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" android:theme="@style/AppTheme" > <activity android:name=".AdMobActivity" android:label="@string/title_activity_ad_mob" > <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> </manifest>
Adding Ads to Layout XML
First way to add AdMob ad is by adding AdView to your XML layout.
Parameter that need to be specified:
- ads:adUnitId : your AdMob app publisher ID
- ads:adSize : ads size
- ads:testDevices : list of testing devices (so only displaying dummy ads) – below there will be explanation how to know your device ID.
- ads:loadAdOnCreate : boolean – ads will be automatically requested on layout displayed
activity_admob.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="@string/hello_world" tools:context=".AdMobActivity" /> <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" ads:adUnitId="a14ff402be4457c" ads:adSize="BANNER" ads:testDevices="TEST_EMULATOR" ads:loadAdOnCreate="true"/> </LinearLayout>
Adding Ads via code
Adding AdView at runtime is as simple as adding other Views. Same like adding in XML you will need to specify the publisher-ID, ads size and test device list.
AdMobActivity.java
package gnu.jimmod.android; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.LinearLayout; import com.google.ads.AdRequest; import com.google.ads.AdSize; import com.google.ads.AdView; public class AdMobActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_admob); LinearLayout layout = (LinearLayout) findViewById(R.id.layout); AdView adView = new AdView(this, AdSize.BANNER, "a14ff402be4457c"); layout.addView(adView); AdRequest adRequest = new AdRequest(); adRequest.addTestDevice(AdRequest.TEST_EMULATOR); adView.loadAd(adRequest); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_admob, menu); return true; } }
How to get my phone device-ID
If you want to add your phone as test device, you will need to run your project with your phone via USB.
Once you run it on your phone, there will be log in Eclipse LogCat window about your device-ID
Then you can copy and paste it to your code.
Screenshot
Summary
Basically adding ads to your application is simple. We just need to be creative on how and when displaying the ads.
I’m sure I will write more about ads on android, especially on ways to display ads on our apps.
Until then..
Thank you for reading, hope this article able to help you.
Source Code
Download the tutorial source code here:
https://bitbucket.org/jimmod/blog_android_admob/changeset/53f720effced
Related posts:



September 24th, 2012 on 9:00 pm
Gan ini pake adt berapa ko saya coba gk bisa ya
September 25th, 2012 on 3:52 pm
Hm.. gak afal pas bikin artikel ini pakai yang versi berapa.
Tapi yang terbaru saat itu, antara 18 ato 20.
Ads nya tidak muncul atau ada error?
December 10th, 2012 on 9:05 am
Hi, i need to do the same, but with Netbeans.
April 18th, 2013 on 10:50 pm
Gan, kalo bikin apk dr publisher id tuh gmn cara ny gan. tengkiu.
April 24th, 2013 on 12:51 am
@euler:
For netbeans you can use the same code.
@Galih:
Bikin APK kalo pake Eclipse tinggal klik kanan di project – android tools – export signed package