- First you'll need to make sure you've got the latest version of the Android SDK.
- Next, you need to download the AdMob SDK. Inside of the zip file, you'll find a .jar file. Copy that to the libs folder under your project root.
- Now go to your project in Eclipse. Right click your project in the package explorer and choose refresh to make sure the jar is showing up in your libs folder.
- Next we need to add the library to the project. To do this, right click your project and go to properties. Then go to "Java Build Path" on the left and then choose the "Libraries" tab. Now click "Add JARs..." and you should see the GoogleAdMobAdsSdk jar file inside of your project. Select that and click OK until you're back to the editor.
- Next we need to add an AdActivity to our AndroidManifest.xml file. Put this at the bottom of the application section, just before </application>.
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
- You also need to set the permissions to access the Internet and the Network State. Obviously, these are required to download the ads. This should be pasted at the end of your AndroidManifest.xml right before the </manifest> tag.
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- Now it's time to add the ad to your application. The library we included comes with a new view called an AdView. Let's add this to our layout through XML. Open the main layout xml file for your project. In this example, we'll use a linear layout. We'll begin by adding the namespace (the xmlns:ads="http://..." part) to the LinearLayout as shown below.
<LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:background="#FFFFFF" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
- Now go to the bottom of the LinearLayout and add the AdView code shown below. Remeber to insert your publisher id into the adUnitId field.
- The last tricky part is to use wrap_content for your ad height and then set the main view element to a height of 0px and weight of 1. This will make the AdView size to the banner ad size and then will allow your main content to take up the remaining space. In my project, I had a linear layout that was set to fill_parent. Inside of that I had a fragment with a height of 0px and weight of 1 followed by the AdView with a height of wrap_content.
One thing to look out for for those of you that are running a custom mod on your phone is the etc/hosts file. Many mods include a list of ad domains that are blocked through the hosts file. This is great if you don't like seeing ads, but can be a problem when it comes time to test ads in your app. I used notepad++ and a find/replace with a regular expression like the one below.
# \1This will comment out all of the admob lines. I also did it with a search for google instead of admob.
To get your admob ad unit id go to your admob account and click on Sites & Apps and then click the manage button underneath the app you are working with. You'll see the Publisher ID for the app on the details page. This is the Ad Unit ID.
|Admob Administration page for an app.|