Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

How to integrate Admob into your app

Problem:

You want to monetize your free app by showing ads within it.

Solution:

Using Admob Libraries you can start using ads in your free app, getting money for each click.

Discussion:

AdMob is one of the world's largest mobile advertising networks, offering solutions for discovery, branding and monetization on mobile phones. The AdMob Android SDK contains the code necessary to install AdMob ads in your application.
1==
In your project's root directory create a subdirectory libs. This will already be done for you if you used Android's activitycreator tool. Copy the AdMob JAR (admob-sdk-android.jar) file into that libs directory. For Eclipse projects: #Right-click on your project from the Package Explorer tab and select '''Properties''' #Select '''Java Build Path''' from left panel #Select '''Libraries''' tab from the main window #Click on '''Add JARs...''' #Select the JAR copied to the libs directory #Click '''OK''' to add the SDK to your Android project
2==
Add your publisher ID to your ''AndroidManifest.xml'' . Just before the closing ''</application> '' tag add a line to set your publisher ID. If your publisher ID were 149afxxxx, the line would look like this: To find your publisher ID, log into your AdMob account, select the '''Sites and Apps''' tab, and click on the '''Manage Settings''' link for your site. On this page, you can find your publisher ID as shows figure 1. [[Image:figure1.png ]] ''figure 1. Showing where you can find your Publisher ID''
3==
Add the INTERNET permission to your ''AndroidManifest.xml'' just before the closing </manifest> tag: Optionally, you can add the ACCESSCOARSELOCATION and/or ACCESSFINELOCATION permissions to allow AdMob the ability to show geo- targeted ads. Your final AndroidManifest.xml may look something like figure 2. [[Image:figure2.png ]] ''figure 2. This is how it would look after pasting some code''
4==
Paste the following into your attrs.xml file: If your project does not already have an attrs.xml file, then create one in the /res/values/ directory of your project, and paste the following:
5==
Create a reference to the attrs.xml file in your layout element by adding xmlns line that includes your package name specified in AndroidManifest.xml . For example, if your package name were com.example.sampleapp you would include this line: xmlns:myapp="http://schemas.android.com/apk/res/com.example.sampleapp" So for a simple screen with only one ad, your layout element would look like this: width="fillparent" android:layoutheight="fillparent"> width="fillparent" android:layoutheight="wrapcontent" myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF" myapp:secondaryTextColor="#CCCCCC" />
6==
When integrating AdMob ads into your application it is recommended to use test mode. In test mode test, ads are always returned. Test mode is enabled on a per-device basis. To enable test mode for a device, first request an ad, then look in LogCat for a line like the following: To get test ads on the emulator use AdManager.setTestDevices... Once you have the device ID you can enable test mode by calling in your main activity AdManager.setTestDevices: AdManager.setTestDevices( new String[] { AdManager.TESTEMULATOR, "E83D20734F72FB3108F104ABC0FFC738", //Phone ID } ); } Once you have successfully requested test ads, try clicking on each type of test ad to make sure it works properly from your application. The type of test ad returned is changed with AdManager.setTestAction. You can see the result in figure 3. [[Image:figure3.png ]] ''figure 3. The result of this snippet''

See Also:

http://www.admob.com/ http://androidtitlan.org/2010/09/como-agregar-publicidad-con-admob-a-tu-android-app/ http://groups.google.com/group/admob-publisher-discuss

Downloads:

(not handled yet)