This tutorial explains you that how to add Google mobile ads to your application. AdMob provides you text, image banners and full screen ads. The following steps are required to get ads..
1. First, you should have a Google account. If have already, then login to AdMob site.
2. Add new app in AdMob site and get the Publisher ID.
3. Add AdMob SDK to your Android Apllication.
4. Some code required.
How to get your Publisher ID from AdMob Site?
First, you should have a Google account to add new app in AdMob site.
After logging, click on Sites & Apps and then select Add Site/App. Refer below pic.
Then select your platform and fill the fields to complete the registration.
Now, AdMob SDK added to your application.
Code:
Android Manifest file:
Add Internet and Access Network permissions in manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Declare com.google.ads.AdActivity.
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
Banner Ads:
Banner Ads used to display in small portion of screen. It available in different sizes for different devices. See table for available sizes.
Create your Banner in XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_Publisher_ID"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
</RelativeLayout>
Either you can do it in Activity also.
MainAcitivity code:
public class MainActivity extends Activity {
private AdView adView;
String MY_AD_UNIT_ID = "a152xxxxx3bcb3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, AdSize.SMART_BANNER, MY_AD_UNIT_ID);
adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
}
The banner output looks like below screen.
Interstitials Ads:
Interstitials ads will displays in full screen. But banners will takes a small portion of screen. Interstials ads are richer and heavy weight. It mostly used in loading pages.
Code:
public class InterstitialAdsActivity extends Activity implements AdListener{
String MY_AD_UNIT_ID = "a152xxxxxxbcb3";
private InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the interstitial
interstitial = new InterstitialAd(this, MY_AD_UNIT_ID);
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
}
/** Called when an ad is received. */
@Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
/** Called when an ad is clicked and about to return to the application. */
@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "onDismissScreen", Toast.LENGTH_SHORT).show();
}
/** Called when an ad was not received. */
@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode arg1) {
// Change the button text and disable the button.
if (ad == interstitial) {
Toast.makeText(this, "Failed to Receive Ad", Toast.LENGTH_SHORT).show();
}
}
/**
* Called when an ad is clicked and going to start a new Activity that will
* leave the application (e.g. breaking out to the Browser or Maps
* application).
*/
@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
if (ad == interstitial) {
Toast.makeText(this, "Application ended", Toast.LENGTH_SHORT).show();
}
}
/**
* Called when an Activity is created in front of the app (e.g. an
* interstitial is shown, or an ad is clicked and launches a new Activity).
*/
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
if (ad == interstitial) {
Toast.makeText(this, "Present Screen", Toast.LENGTH_SHORT).show();
}
}
}
The output looks likes below screen...
Note: Download samples from developers.google.
Frequent errors:
1.If you get Runtime Exception error, then check your JAR file version or make sure that your manifest file contains all the required details are correct.
2. Uncaught ReferenceError: AFMA_getSdkConstants is not defined : Ice Cream Sandwich won't allow network operations in Main thread. Load it in separate thread.
adView = new AdView(this, AdSize.SMART_BANNER, MY_AD_UNIT_ID);
adView = (AdView)this.findViewById(R.id.adView);
(new Thread() {
public void run() {
Looper.prepare();
adView.loadAd(new AdRequest());
}
}).start();
Otherwise, check your network or internet connection once.
Enjoy with your ads............
1. First, you should have a Google account. If have already, then login to AdMob site.
2. Add new app in AdMob site and get the Publisher ID.
3. Add AdMob SDK to your Android Apllication.
4. Some code required.
How to get your Publisher ID from AdMob Site?
First, you should have a Google account to add new app in AdMob site.
After logging, click on Sites & Apps and then select Add Site/App. Refer below pic.
Then select your platform and fill the fields to complete the registration.
Then Note down your Publisher ID from Manage settings.
Copy your Publisher ID and paste it in your code.
Add AdMob SDK to your Application.
First, check your AdMob SDK library installation in Android SDK Manager. If not installed then install it.
Open application properties. In JAVA Path, click Add External JAR. Get your AdMob JAR file from your Android SDK like D:\android-sdks\extras\google\admob_ads_sdk.
After adding JAR file. Select Order and export and check the GoogleAdsAdmobSDK JAR. Click OK to finish.
Code:
Android Manifest file:
Add Internet and Access Network permissions in manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Declare com.google.ads.AdActivity.
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" >
</activity>
Banner Ads:
Banner Ads used to display in small portion of screen. It available in different sizes for different devices. See table for available sizes.
Create your Banner in XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_Publisher_ID"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
</RelativeLayout>
Either you can do it in Activity also.
MainAcitivity code:
public class MainActivity extends Activity {
private AdView adView;
String MY_AD_UNIT_ID = "a152xxxxx3bcb3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, AdSize.SMART_BANNER, MY_AD_UNIT_ID);
adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
}
The banner output looks like below screen.
Interstitials Ads:
Interstitials ads will displays in full screen. But banners will takes a small portion of screen. Interstials ads are richer and heavy weight. It mostly used in loading pages.
Code:
public class InterstitialAdsActivity extends Activity implements AdListener{
String MY_AD_UNIT_ID = "a152xxxxxxbcb3";
private InterstitialAd interstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the interstitial
interstitial = new InterstitialAd(this, MY_AD_UNIT_ID);
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
}
/** Called when an ad is received. */
@Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
/** Called when an ad is clicked and about to return to the application. */
@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "onDismissScreen", Toast.LENGTH_SHORT).show();
}
/** Called when an ad was not received. */
@Override
public void onFailedToReceiveAd(Ad ad, ErrorCode arg1) {
// Change the button text and disable the button.
if (ad == interstitial) {
Toast.makeText(this, "Failed to Receive Ad", Toast.LENGTH_SHORT).show();
}
}
/**
* Called when an ad is clicked and going to start a new Activity that will
* leave the application (e.g. breaking out to the Browser or Maps
* application).
*/
@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
if (ad == interstitial) {
Toast.makeText(this, "Application ended", Toast.LENGTH_SHORT).show();
}
}
/**
* Called when an Activity is created in front of the app (e.g. an
* interstitial is shown, or an ad is clicked and launches a new Activity).
*/
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
if (ad == interstitial) {
Toast.makeText(this, "Present Screen", Toast.LENGTH_SHORT).show();
}
}
}
The output looks likes below screen...
Note: Download samples from developers.google.
Frequent errors:
1.If you get Runtime Exception error, then check your JAR file version or make sure that your manifest file contains all the required details are correct.
2. Uncaught ReferenceError: AFMA_getSdkConstants is not defined : Ice Cream Sandwich won't allow network operations in Main thread. Load it in separate thread.
adView = new AdView(this, AdSize.SMART_BANNER, MY_AD_UNIT_ID);
adView = (AdView)this.findViewById(R.id.adView);
(new Thread() {
public void run() {
Looper.prepare();
adView.loadAd(new AdRequest());
}
}).start();
Otherwise, check your network or internet connection once.
Enjoy with your ads............