Quick Guide For Admob

A few weeks ago, i was trying to integrate Admob in an application that i was writing.  After a few hours later i found a way…I will show you a very simple implementation of this below. You can find many examples in xml format out there but I prefer an alternative way to do it so, all the integration will be in code.

I assume that you already have an Admob account. If not, click here and make one.  After the creation of your account, login and select add site/app and  continue with the registration.

There are two parts to make it work.

First we have to add the code below in the AndroidManifest.xml, right after the close tag of our activity/activities.

<meta-data android:value="ID" android:name="ADMOB_PUBLISHER_ID" />
 <activity android:name="com.admob.android.ads.AdMobActivity"
 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
 android:configChanges="orientation|keyboard|keyboardHidden" />

From the code above, you can see that we declare a new activity for the Admob and also a meta tag. The only change you have to do, so you can use it for your code, is  to change the android:value=”ID”  of the meta tag. Change this to the value that Admob gave you when you register your app. It should look like this a14b9a7t22u3982.

Because Admob uses Internet connection to display ads, you should add and the appropriate permission to the Manifest. Copy and paste the code below exactly before your application tag starts.

<uses-permission android:name="android.permission.INTERNET"/>

We have finished the first part of the Admob for the AndroidManifest.xml so let’s see what we have to include in our code – main activity to make it work.

In onCreate() method copy/paste the following code

LinearLayout ll = new LinearLayout(this);

 ll.setOrientation(LinearLayout.VERTICAL);
 ll.setGravity(android.view.Gravity.BOTTOM);

 addContentView(ll, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));

 AdView adView = new AdView(this);
 ll.addView(adView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

 adView.setBackgroundColor(0xff000000);
 adView.setPrimaryTextColor(0xffffffff);
 adView.setSecondaryTextColor(0xffcccccc);
 adView.setKeywords("Your keywords here");
 adView.setRequestInterval(15);
 adView.requestFreshAd();

First we create a LinearLayout and we set its properties. I have used the setGravity method with the value android.view.Gravity.BOTTOM so i can have the ads at the bottom of my app. If you want your ads at the top of the app just comment or delete this method.

Then i used the addContentView to make the ads displayed above the content of my app and not as a separate view.  The rest of the code is the standard properties for the Admob that is commonly used in xml attributes but i have converted into code.

Because the ads won’t show after you have done all the above, and actually they will need much more time before they show up, this is somehow defined from the time and the downloads your app has in the market, you can test if you get ads by using the test mode of Admob which will always send you ads. If you don’t receive these then you had done something wrong…

To enable the test ads just add the following code in your onCreate method

AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR,"369xxxxxxxxxxxxxxxxxxxxxxxxxxx" } );

As you see from the code above, i set as test devices the Android Emulator and my phone by using my device serial number, because i want to test the app and on my phone. You can find your id from the settings of your phone.

Finally, be sure you have comment the test line (the line of code above) before you release your app in the market so your users sees the real ads.

I hope this quick guide helps someone out there….

Have Fun!!!

Leave a Comment