AdMob Tutorial – Using AdMob SDK

image

 

A few weeks ago I posted a tutorial using one of the two ways to configure AdMob in your Android SDK apps. This approach utilized an alternate SDK. For this tutorial I will walk through the SDK provided on the AdMob site. I use Eclipse, so all my tutorials will reference that.

Start by creating a new Android project. Once that’s ready, you can go through the following steps to setup AdMob.

  1. Add the AdMob SDK to the project
  2. Configure the AndroidManifest.xml file
  3. Add an attrs.xml file
  4. Setup the layout file
  5. Request a test Ad

AdMob SDK

  • In Project Explorer, right-click your project and create a new folder called “libs”
  • Now you can simply drag ‘n drop “admob-sdk-android.jar” into this libs folder from Windows Explorer
  • In Project Explorer, right-click your project and click properties.
  • Click Java Build Path on the left
  • Click the Libraries tab on the right
  • Click Add JARs
  • Expand the project you are working with, select “admob-sdk-android.jar” in the libs folder, and click OK. Click Ok again.
  • You should now see an entry added to your project with the name of the jar file above. See screenshot below

image

AndroidManifest.xml

  • Double-click AndroidManifest.xml in your project to open it
  • Select the last tab on the right to get the xml view
  • Before the closing tag, add the following
"YOUR_PUBLISHER_ID" android:name="ADMOB_PUBLISHER_ID" />

"com.admob.android.ads.AdMobActivity"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

android:configChanges="orientation|keyboard|keyboardHidden" />

"com.admob.android.ads.analytics.InstallReceiver"

android:exported="true">

"com.android.vending.INSTALL_REFERRER" />



  • Every Site/App you create in AdMob will have a unique ID.
  • To obtain your ID, logon to AdMob
  • Go to the Sites & Apps tab
  • Mouse-over the app that you want to get the Id for and a couple options will show up
  • Click Manage Settings
  • image
  • On the next page you will see the publisher Id at the top
  • image
  • You can simply select it with the mouse and use copy/paste
  • Replace YOUR_PUBLISHER_ID, with your selected Id.

By the way, directly to the right of the Id is “Get Publisher Code”. If you click this it will take you to a page where you can download the AdMob SDK which contains the .jar file mentioned earlier.

  • After the closing tag, add the following

"android.permission.INTERNET" />

Technically, most if not all your apps will use the above permission. Unless your app does not require internet access of course.

attrs.xml

  • In Eclipse, expand the “res” folder of your project, and the “values” folder
  • Right-click the “values” folder and create a new file
  • Name the file attrs.xml and click Finish
  • Double-click this new file to edit it
  • The file will initially be empty and you will need to add the following. (This is direct from the SDK guide)
"1.0" encoding="utf-8"?>

        "com.admob.android.ads.AdView">

        "backgroundColor" format="color" />

        "primaryTextColor" format="color" />

        "secondaryTextColor" format="color" />

        "keywords" format="string" />

        "refreshInterval" format="integer" />

    


 

This configures the appearance of the ads. You can actually override this directly from your AdMob “App Settings” tab from the same location where you obtained the Publisher Id.

image

Just select “Use colors set below:” and adjust as needed. Don’t forget to click “Save Settings” on the page when done.

Layout

  • Back in Eclipse again, expand your project, res—>layout
  • Double-click main.xml to edit it, and click the main.xml tab at the bottom to open the xml view
  • Again, directly from the SDK guide, this example shows an app that has a layout with only an Ad showing. The following should be placed in main.xml
"1.0" encoding="utf-8"?>

    

        xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:myapp="http://schemas.android.com/apk/res/com.taiic.admob.template"

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

        

            android:id="@+id/ad"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            myapp:backgroundColor="#000000"

            myapp:primaryTextColor="#FFFFFF"

            myapp:secondaryTextColor="#CCCCCC"

        />


 

  • The line that starts with “xmlns:myapp=” needs to have the package name of your app entered after the last “/” (forward slash)
  • Above it contains the name of my template project I used for this tutorial “com.taiic.admob.template”
  • You can find the package name in the AndroidManifest.xml. Just look for “package=” in this file.

Test Ad

Now we’re ready to request a test Ad.

  • Expand your project in Eclipse and expand the “src” folder
  • Expand the package name and double-click the .java file below that to edit it. This is the main activity file. In my template project it’s called adMain.java
  • Add the following below the line that starts with, “setContentView” (This is directly from the SDK)
AdManager.setTestDevices( new String[] {

        AdManager.TEST_EMULATOR, // Android emulator

        "EA4D5F882B98D4DA8CAAB831A8124698", // Device ID of the device you want to test on

        } );

  • When you enter the above lines, you will get an error on “AdManager”
  • Just mouse-over it, and left-click “Import ‘AdManager’ (com.admob.android.ads)”
  • image
  • To test with the emulator comment out or remove the line with the device ID. To test with a real device, commend out or delete the line “AdManager.TEST_EMULATOR.
  • If you’re testing with a real device, you will need to get it’s ID. To do this, run the app with logcat running, or view logcat in DDMS in Eclipse.
  • After it requests an add locate the line that starts with, “To get test ads on this device use AdManager.setTestDevices”. At the end of this line you will find the device ID. Just copy/paste that ID into the code in the .java file.
  • When the app starts, you should see a test Ad

image

To get live ads, all you need to do is comment out or remove all the lines that request a test Ad, leaving only the following.

AdView adView = (AdView)findViewById(R.id.ad);

adView.requestFreshAd();

 

You can find this template project in my downloads: http://taiic.com/downloads

 

Join our community on facebook. Just hit the Like in the left sidebar (towards the top of the page), this button

Or directly on our fb page at http://www.facebook.com/pages/tAIICcom/116243311786911

You may also like...

  • Hate to be a pain but this is already out of date. Admob have released a new SDK that no longer supports .setTestDevices() and .requestFreshAd(). I’d say its from the merger with AdSense. Sadly, I can’t find a workaround (yet) for the test ads – and yet we still need to see the ads during development for the sake of designing our screen around them. I can hide the ads of course, which doesn’t help – from memory AdSense is exactly the same…

    • Thanks for the tip. I haven’t looked at their supplied SDK in a while. I just used the one I had locally. I’ll definitely check it out.

      • Would be really nice if you could make a tutorial for the newest version.. can’t get it to work 🙁

        • Definitely, I’ll try and get this out soon.

  • Guest

    How do I get this magic “device id” so that only test ads are displayed?  (My app is currently displaying REAL ads all the time.)

    A few of my test devices are 1000 miles from here… so there’s no possible way I can “hook them all up to Eclipse/DDMS and view the log”.  Is that the ONLY way?

    What is the java code method to “get the device Id that setTestDevice needs”????

    All my SDKs, libs, executables are up-to-date as of today.