Monday, April 16, 2012

How to integrate AdMob ads in android application.

In this post I show how to integrate ads in your android application.

 Step 1:

 Open admob.com in your browser.



Step 2:

 Log in to admob site using admob account or google account.




Step 3:

Fill the details.





step 4:

 Click on Add your first App. Below Monotize Your mobile apps







Step 5:

 Fill your Bank details. (For Bank SWIFT code visit your bank website.)




 Step 6:

Select Android App icon from Add Site/App. Fill the details below about your app. Click continue.









Step 7:

Click on Admob Android SDK to download the sdk zip file and extact it.

Step 8:

We can integerate ads in our application in two ways.

1. Using XML file

 2. Using java code

In this post I show how to integrate ads using XML code. Open your project in Eclipse IDE.

Step 9:

 Right click on your project and select properties

Step 10:

select java build path > Libraries > Add External jar. browse the jar file (GoogleAdMobAdsSdk-4.1.0 or other versions) click ok.




Step 11:

open your project mainfest file. Add the following lines of code. before application tag.
To load ads we need internet permission.

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


Step 12:

 Add an activity to your manifest file.

<android:name="com.google.ads.AdActivity"
activity android:configchanges="keyboard|keyboardHidden|orientation" >

 Step 13:

 Open your main.xml file or any other xml file where you want to place the ads. Add the following code in your layout tag after "xmlns:android="...." "

 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

or

 Replace your layout with the following code

<linearlayout
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>

replace your_admob_id with your admob publisher id.

To get admob publisher id click on your application name in admob site and select manage settings.
Copy your id and paste it in adview tag.

Note:
publisher id changes from one application to another application. Make sure that your package name in eclipse and your package name in admob is same. Other wise it won't work.


Step 14:

 Run your application 
You will see the ads in your Emulator..

Note:

Don't click on your ads in Emulator.. If you click your account will be closed for clicking self ads ..