Integrating New Google Admob With Banner And Interstitial Ads
Last Updated: July 8, 2014
In this tutorial, you will learn how to integrate the new Google Admob with banner and interstitial ads into your Android application. The new AdMob is a streamlined user interface, to make it even easier for you to monetize and promote your apps in minutes. Maximize your earnings with the new AdMob improved tools to help app developers build thier business. We will create a simple view that launches an Interstitial on start and show a single banner advertisement. So lets begin…
GETTING STARTED
Sign up as an Admob Publisher here. Log in to your dashboard once you have your account approved.
Identify your publisher ID on the top right of your dashboard.
Open the Monetize tab and create an ad unit for your application. You should be able to see your ad unit after selecting an app on your left panel.
Download the new Google Play Services Library using the Android SDK Manager in your Eclipse IDE.
Import Google Play Services Library into your Eclipse IDE. I’ve found mine in D:\Eclipse\sdk\extras\google\google_play_services\libproject . You will have to search the folder yourself.
Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project NewAdmobTutorial.
Application Name : NewAdmobTutorial
Project Name : NewAdmobTutorial
Package Name : com.androidbegin.newadmobtutorial
Import Google Play Services Library into your App.
Open your MainActivity.java and paste the following code.
package com.androidbegin.newadmobtutorial; 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; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { private InterstitialAd interstitial; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from activity_main.xml setContentView(R.layout.activity_main); // Prepare the Interstitial Ad interstitial = new InterstitialAd(MainActivity.this); // Insert the Ad Unit ID interstitial.setAdUnitId("ca-app-pub-123456789/123456789"); //Locate the Banner Ad in activity_main.xml AdView adView = (AdView) this.findViewById(R.id.adView); // Request for Ads AdRequest adRequest = new AdRequest.Builder() // Add a test device to show Test Ads .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("CC5F2C72DF2B356BBF0DA198") .build(); // Load ads into Banner Ads adView.loadAd(adRequest); // Load ads into Interstitial Ads interstitial.loadAd(adRequest); // Prepare an Interstitial Ad Listener interstitial.setAdListener(new AdListener() { public void onAdLoaded() { // Call displayInterstitial() function displayInterstitial(); } }); } public void displayInterstitial() { // If Ads are loaded, show Interstitial else show nothing. if (interstitial.isLoaded()) { interstitial.show(); } } }
In this MainActivity.java, a test device is being used to run test ads. Its highly recommended to test your ads using test ads to prevent invalid clicks that will cause suspension of your AdMob publisher account. To generate a Test Device ID, simply type in some random text into addTestDevice as shown below.
// Request for Ads AdRequest adRequest = new AdRequest.Builder() // Add a test device to show Test Ads .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("abc") //Random Text .build(); // Load ads into Banner Ads adView.loadAd(adRequest);
Run your app, and filter your logcat with the word “ads” as shown below. You should be able to generate some unique strings.
Now paste the Test Device ID into addTestDevice. You have to generate your own Test Device ID, because each Android Device has an unique ID.
Showing Real Ads
To show real ads, just remove the two lines by commenting it as shown below.
AdRequest adRequest = new AdRequest.Builder() //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) //.addTestDevice("abc") .build(); adView.loadAd(adRequest);
Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file activity_main.xml and paste the following code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-123456789/123456789" /> </LinearLayout>
In your AndroidManifest.xml, we need to declare an activity for Google Play Services and permissions to allow the application to access to the Internet and check network status. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidbegin.newadmobtutorial" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </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" /> </application> </manifest>
Output:
[purchase_link id=”8027″ text=”Purchase to Download Source Code” style=”button” color=”green”]
igot this error in xml in the given below lineError parsing XML: unbound prefix<com.google.android.gms.ads.AdView
Rmn Nizam
Integrating New Google Admob With Banner And Interstitial Ads
How to set adblock button ?
Hirpara Vivek
Integrating New Google Admob With Banner And Interstitial Ads
all the ads example with the source code in this app :- https://play.google.com/store/apps/details?id=manu.r06.AdmobAdsExample
Mritunjay Rawat
Integrating New Google Admob With Banner And Interstitial Ads
How to hide the close icon on ads on android
pocket user
Integrating New Google Admob With Banner And Interstitial Ads