Integrating New Google Admob with Banner and Interstitial ads

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

packagecom.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;

publicclassMainActivityextendsActivity{

privateInterstitialAd interstitial;

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

// Get the view from activity_main.xml

setContentView(R.layout.activity_main);

// Prepare the Interstitial Ad

interstitial=newInterstitialAd(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=newAdRequest.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(newAdListener(){

publicvoidonAdLoaded(){

// Call displayInterstitial() function

displayInterstitial();

}

});

}

publicvoiddisplayInterstitial(){

// 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.

1

2

3

4

5

6

7

8

9

10

// Request for Ads

AdRequest adRequest=newAdRequest.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.

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.

Yaa, it’s working and superb demo, its really so easy for understating.

Paweł Lewiński

Hello, great tutorial which worked for me. I have a question what changes can I make to have the small banner only, without interstitial? Thanks in advane.

Vamsi Challa

Use code only related to Banner Ads from the above example

asdssa

I really love you Thank you very much really really Thank you

Govind Bhanushali

I followed the same steps and did all as step by step, but i can not see any ads in banner. and for interstitial, it never showing ads. Please suggest where i am doing wrong, the code is completly the same as above. i can see the sessions and all in adMob site report too. but banner is not coming with any ads. it simply grey blank banner

sd vipeen

Did u switch on internet connection in ur mob.?
bcas even i had the same prob..

Vivek Gandhi

Does AdMob work properly in JellyBean? I was able to load Ads in IceCream Sanwich and Kitkat, but not in jellybean. Someone please advice me

Onwuasoanya Ndubuisi George

Worked perfectly. This is fantastic.

pakistantutor

It did not work for me.
I see some unknown errros in MainActivity
Android Manifest and others

http://www.AndroidBegin.com/ AndroidBegin

Hi, could you please paste your errors on pastebin.com for us to have a look at your issue? Thanks

suresh

Thanx buddy. It really helped.

http://cobadibanting.blogspot.com Junico Rudyanto

how about its?
+ no error
+ google-play-services_lib ” Done ”
+ using eclipse juno..
– undetect google play service in library

i have to follow your code about .xml, .java, & android manifest but but the ad has still not shown

Guest

Right click on Google plays services lib project -> android.
Click on “as library” then try to readded it to your project.

Hi, great tutorial, many thanks, but i have a questions.
My app must be in play store to have a statistic? Beacuse I add my own add id and it’s show with Your code, but in admob account i don’h have any stats.

Łukasz Żymła

sorry, now i have stats, they show after 24h

LoKey

Hi, I am tying to incorporate this in my Java fragment extended class.. The code below: interstitial = new InterstitialAd(HomeFragment.this);
is giving me this error message:
The constructor InterstitialAd(HomeFragment) is undefined……

Do you know why?

MattyDeBie

try interstitial = new InterstitalAd( getActivity() );

manideep

I have tried the same example by replacing interstitial.setAdUnitId(“ca-app-pub-123456789/123456789″); with my id , it was throwing null pointer exception at adView.loadAd(adRequestBuilder.build());

Ajay Kumar

Hi., can i add my account details in admob., or is it any problem., bca admob asking my CVV and expiry date also.,pls help me

Alexander Nowak

It’s your credit card information

John Hillen

Hello, I tried your example and getting any Warnings and a Error code 0 after loading the ad. I use the latest google service api revision 22. I have set the Ad Unit ID for interstitial in the MainActivity and the Ad Unit ID in the main_activity.xml and copied the number for the test device from LogCat. But for all times no Ads are loaded.

Additional I tried the google play service from your example, but with the same error.

01-1522:27:47.461:W/AwContents(30510):onDetachedFromWindow called when already detached.Ignoring

01-1522:27:47.476:W/chromium(30510):[WARNING:proxy_service.cc(901)]PAC support disabled because there isno system implementation

01-1522:27:47.482:W/Ads(30510):Failed toload ad:0

Vamsi Challa

Easy and precise as it should be.. Nice!!!

Terrence Markle

Hello All… This was a very good tutorial. However, I am having a problem. I have copied the MainActivity.java, activity_main.xml, and AndroidManifest.xml (minSdk=9 and targetSdk=19) directly into the Eclipse project “NewAdmobTutorial” as presented in this tutorial and checked all the code. When I run the project (Run -> Run As -> Android Application), I get the error “Android library projects cannot be launched”. I Google searched for a solution but nothing worked. I used the Android SDK Manager to install Google Play Services (Rev. 22) and imported the “google-play-services.jar” into the Eclipse project as an Android Private Libraries.The “is Library” box is checked in the project’s Property -> Android -> Library. I checked the Java Build Path and (1) in the Order and Export section the Android Private Libraries box is checked and (2) in the Libraries section the google-play-services.jar is included in the Android Dependencies and Android Private Libraries. I do not know what to try next. I hope someone can provide a solution for me.

http://www.AndroidBegin.com/ AndroidBegin

Hi Terrence, I understand your frustration. Before you try this tutorial, you have to know how to import libraries and configure your project properties. Copy and paste might work sometimes. I suggest you start with some of our less complicated tutorials.

Terrence Markle

Hi AndroidBegin,… Thank you for your response. Yes, I am frustrated. I’ve been trying to get the Admob feature working for about a week.
1. I followed all of Google’s docs for installing the Google Play Services Library using the Android SDK Manager in Eclipse IDE.
2. I setup your project (Integrating New Google Admob…) by manually typing each line of code into Eclipse (no errors encountered).
3. I imported the google-play-services.jar (as an Android Private Library) into the project.
4. I checked the Java Build Path and it looks OK.
5. When I ticked (check marked) the “is Library” box in the Property/Android/Library and tried to run the project, I got the error “Android library project cannot be launched”.
6. When I removed the tick mark in “is Library” and tried to run the project, Eclipse displayed “Process system isn’t responding”.
7. Question #1: Should the “is Library” box be ticked or not ticked?
8. Question #2: Where do I find the tutorial that you mentioned that will help me solve this problem? I appreciate any help you can give me.
Best regards,
Terry

https://www.facebook.com/wajid.ali.52?ref=tn_tnmn Wajid Ali

dear Sir I have same problem.. but can’t find Solution…. when I run the Project then error show

Please stop telling people to call interstitial.show() from onAdLoaded(). It will get their Admob accounts banned.

http://www.AndroidBegin.com/ AndroidBegin

Hi William Ferguson, could you please elaborate more on your comment? or maybe some references? Thank you

William Ferguson

Admob has always said that interstitial ads should be shown at a natural break point in your app. Showing them when the ad has finished loading means that you have no control over when they will be shown. Typically it will be 10-40 seconds after you called load() which means that the user gets interrupted in the middle on their task – perhaps the game they are playing. It is *very* poor UX and Admob regularly bans accounts for exactly this practice. I’m speaking with some good understanding of the issue as I am a Google Developer Expert for Admob.

http://www.AndroidBegin.com/ AndroidBegin

Thank you for your time and its my pleasure to welcome you to my blog. I will make sure this valuable comment being noted by all developers that visited this tutorial. Thanks and regards.

DanielVAVAVAVAVAVAVAVA

Hello willian , i see u have experience on google ads, can u please write me on email an teach how to add interstitital ads on start and exit of my android app, i’m new and step by step guide will be very helpful to me ::D

An ad on exit will be extremely difficult to pull off, and an ad on start will not be the best user experience because it takes takes for the ad to load. You are better off finding a natural break point in your app. If your app doesn’t have one, then I would suggest that interstitials are not a good option for your app. https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial

DanielVAVAVAVAVAVAVAVA

i want interstitial add to show 3 sec after user enters on my appliccation, becuse its not a game and i dont have or something like that, if you help me to implement my interstitial ad on my project i will be very thankfull 😀 step by step guide, i saw the examples in google develope but its not what i search for:D than you

Yuvaraj Rajamanickam Coimbator

soon i will teach you.

Joey

What’s the recommended frequency for interstitials? I have a studying app for a major exam in my country where the user spends about 3-7 minutes on a page. My natural break point will be when they’re going to the next page because it’s usually a totally different topic, but what would you consider spamming and what would you consider good UX? I’m currently jobless, I don’t want to be greedy and drive away customers, but I could also use some $$$. Every 5 minutes? 10, 20?

William Ferguson

Put yourself in the mind of your users and start with a value that seems reasonable. Your users will let you know if you’ve got it wrong :-). For break points around the 5 minute mark I like to randomly show an ad about 20-25% of the time. That way they are getting one interstitial on average every 20-25 mins and the pattern is non-determinstic.