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, 108 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.

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