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.

publisherIDAdmob

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.

admobadunit

Download the new Google Play Services Library using the Android SDK Manager in your Eclipse IDE.

SDKManager

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.

GooglePlayServices

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.

Reference

Open your MainActivity.java and paste the following code.

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.

Run your app, and filter your logcat with the word “ads” as shown below. You should be able to generate some unique strings.

testdeviceID

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.

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

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

Output:

AdmobAds

NewAdMobTutorial (4.7 MiB, 150 downloads)
  • mina kumari

    unfortunately app has stopped

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi, I’ve just tested it again on API 4.3 , its running fine. Did you generate your own Device Test ID to test it out first? Show me some logcat errors please. Thanks.

      • mina kumari

        thanks now work

        but only display in emulator when i publish app and install device not load ads only open blank page

        • http://www.AndroidBegin.com/ AndroidBegin

          If it works in your emulator, it should work on your device too. Please double check your Test Device ID.

  • ibrahim

    Thx very much

  • J Ahsan

    Hi got an error.Please help …java.lang.NoClassDefFoundError: com.google.android.gms.ads.InterstitialAd

    • http://www.AndroidBegin.com/ AndroidBegin

      Hi, did you import the Google Play Services Library into your project?

      • J Ahsan

        I have added the google.play.service.libs as a android code—> marked it as a library–>went to my project properties to reference it–> cleaned both project –RUn (ERROR)

        • Deepak Kanyan

          plz check you import

        • TechnoRapper dotcom

          same problem with me

          • Mohamed Ahmed

            is the problem still existing ?

          • Mohamed Ahmed

            is the problem still exist ?

        • Mohamed Ahmed

          is the problem still exist ?

  • bluesky214

    You should have version for latest android studio,much easier to use when importing sdk,libraries and jar files

    • http://www.AndroidBegin.com/ AndroidBegin

      Agreed with Bluesky.

  • Me

    Also getting java.lang.NoClassDefFoundError: com.google.android.gms.ads.InterstitialAd…I have tried for hours to figure out why, but no answer.

  • Amit Kumar Khare

    Working Like a charm. Thanks a lot. :)

    • Sanjay

      Hi Amit,

      This is Sanjay. Can you please send me a sample hello world application integrated with new admob. Please.
      My mail id is sanju.cvrce@gmail.com

      • http://khare.co.in Amit Kumar Khare

        Hello, I sent you. please check your mail. and let me know if it worked for you. Take care.

      • http://khare.co.in Amit Kumar Khare

        sent you on your email. Please check and have fun.

  • Alex

    I do not understand why an error: “The constructor InterstitialAd(FileCategoryActivity) is undefined”?

  • sameer

    my app was removed by google play saying that Interstitial ads may only be displayed inside of the app they came with. So how to achieve this?

  • Sanjay

    Can any one help me in integrating admob with a sample Hello world. Please help me on adding new admob with the app.

    • Mohamed Ahmed

      have you solved the problem ?

  • william aristizabal

    properties android reference error google play service eclipse

  • Sinchan Maitri

    ” Exception raised during rendering: com/google/android/gms/ads/AdView : Unsupported major.minor version 51.0

    Exception details are logged in Window > Show View > Error Log ”

    i am getting this for all the apps. Please suggest me what to do….

  • TC Ibrahim YENER

    Excellent tutorial. Easy to understand and easy to follow.
    Thanks for it.
    Keep up good work.

  • Nelson Osorio

    Thank you!! Great Tutorialt!!

  • alex

    1 error to create apk
    Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/ads/AdRequest$ErrorCode;

    Because this error ?

    • cherryapp

      remove old jar

  • Neil

    I can not get this going for love or money . has anyone got a working demo please
    I am using eclipse juno and can not get the hang of adding admob
    All I get is errors .

  • http://www.itechaleart.com/ itechaleart

    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.

  • 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

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

      • http://cobadibanting.blogspot.com Junico Rudyanto

        resolved thanks :)

    • Lê Mạnh Huy

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

  • Łukasz Żymła

    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?

  • 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());