Overview :
AdMob brings together best-in-class technology in a single platform, so you can gain insights about your users, drive more in-app purchases, and maximize your ad revenue. No longer will you have to rely on a combination of tools or use precious development resources to build your own solution.
Step 1 :
First we add an account in AdMob publisher and Identify your publisher ID on the top right of your dashboard. Click here to sign up the AdMob publisher.
Step 2 :
Create a new project in Eclipse File ⇒ New ⇒ Android Application Project. Fill in the details and name your project AdmobProject.
Step 3 :
Here is a complete example of MainActivity.java
package com.sampleadmob;
import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public class MainActivity extends Activity {
private InterstitialAd interstitial;}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
setContentView(R.layout.activity_main);
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("ca-app-pub-xxxxxxxxxxxx/xxxxxxxxxx");
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {});
displayAd();
}
public void displayAd() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
}
interstitial.show();
}
Step 4 :
In activity_main.xml we declare this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sampleadmob.MainActivity" >
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
// Insert the Ad Unit ID
ads:adUnitId="ca-app-pub-xxxxxxxxxxxxx/xxxxxxxx" />
</RelativeLayout>
Step 5 :
In Android manifest file we declare permissions to allow the internet connection and network state and declare activity for Google play services.
Declare Google services activity :
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
Internet and network state permission :
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Screenshot :

Baalaji is a Software Engineer at Span Technology Services