This post will look at putting adverts into your application. To use this code you will need to sign up to google’s AdMob service. I’m going to assume that you have done all that and have a publisher id that you can use for this example.
OK, so how to go about this. The idea is to create an advert at the bottom of the screen that is invisible before the advert arrives, and slides up from the bottom of the screen when it arrives.
Step 1 is to create a new Android application in eclipse called AdMobExample. When you have done that you need to right click on the project and select Properties. On the Properties for AdMobExample dialog, click on Java Build Path from the list on the right. You should now see something like this:
To add the GoogleAdMobAdsSdk-4.0.4.jar library you need to click on the Add External Jars button and select the jar file (you must install the library yourself by downloading the SDK from the AdMob site.) I installed the SDK to /usr/lib (on Linux), this might be something like C:/Program Files/GoogleAdsMobAdsSdk on Windows.
Now we can start to build the app. First up is the AndroidManifest.xml file. We need add an activity to the <application> so that AdMob can do its thing. Add this just before the </application> tag:
1 2 3 4 | <!-- AdMobActivity definition --> <activity android:name="com.google.ads.AdActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:configChanges="orientation|keyboard|keyboardHidden" /> |
In order for this activity to work it needs access to the internet, and access to the network state of the device. This means that we must request these permissions by adding these lines after the <application> tag:
1 2 3 | <!-- AdMob SDK requires Internet permission --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
Finally, AdMob can use the device’s location when it serves adverts. To enable this feature you need to add the following after the &jt;uses-permission> lines :
1 2 | <!-- Allow AdMob to use the devices location when delivering ads --> <meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" /> |
That completes the changes to the manifest. Now we need to create a layout that will lock the adverts to the bottom of the screen. This is done by using RelativeLayout. Here is the complete main.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/com.example.admobexample" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- This is the main view. Here it is just a simple TextView, but it will probably be another layout in a more complex app --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <!-- Set myapp:adUnitId to be your publisher ID --> <com.google.ads.AdView android:id="@+id/ad" android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:adUnitId="000000000000000" myapp:adSize="BANNER" myapp:refreshInterval="30" /> </RelativeLayout> </RelativeLayout> |
The first RelativeLayout contains the main view, in this example it is just a simple TextView, followed by another RelativeLayout that contains the view for the advert. In terms of the layout, the crucial part of this layout is line 19, setting the android:layout_alignParentBottom element to true tells android to put the AdView at the bottom of the screen. To get the view to deliver your adverts you need to change the myapp:adUnitId value to be your publisher id.
Now we have got our manifest and layout configured all that is left is to request an advert in the onCreate method:
1 2 3 4 5 6 7 8 9 10 | public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Request an ad AdRequest ad = new AdRequest(); ad.setTesting(true); AdView adView = (AdView)findViewById(R.id.ad); adView.loadAd(ad); } |
The setTesting(true) call tells AdMob to simulate adverts in the emulator, but it will still serve real adverts on a device.
If you run this now in the emulator you should see a screen like this:
If you deploy this to a device then it will deliver a real advert.
Download
You can download this example from here.
To add this example to eclipse go to File|New|Android Project, select “Create project from existing source” and then browse to the folder inside of the downloaded zip file.