AdMob Tutorial – Using AdMob SDK
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.
- Add the AdMob SDK to the project
- Configure the AndroidManifest.xml file
- Add an attrs.xml file
- Setup the layout file
- Request a test Ad
- 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
- 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
- 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
- On the next page you will see the publisher Id at the top
- 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
Technically, most if not all your apps will use the above permission. Unless your app does not require internet access of course.
- 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)
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.
Just select “Use colors set below:” and adjust as needed. Don’t forget to click “Save Settings” on the page when done.
- 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
- 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.
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)
- 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)”
- 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
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.
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