Saturday 18 January 2014

Implementing Admob in Android App



Implementing Admob in Android App


Today I will write a code for implement admob in android app step by step..

Step 1 :  Create New App AdmobDemob in eclipse . 

Step 2 : Add google_play_service_library(Explained in step 2.1  ) to your project now no need to add admob library

Step 2.1 : Go to sdk folder >google >google_play_service>libproject >google_play_service_library(this is the path of google_play_service_libary). Now import this library project in your eclipse and add refrence library to your AdmobDemo Project (How to add Library Project explained in Step 2.2)

Step 2.2 :Right Click on your project(AdmobDemo ) go to property >android >Add >Select google_play_service_library click Ok then apply then Ok .

Step 3: put this code in MainActivity .

package com.ritesh.admobdemo;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitAction();
}

private void InitAction() {
// TODO Auto-generated method stub
 AdView adView = (AdView)this.findViewById(R.id.adView);
   AdRequest adRequest = new AdRequest.Builder().build();
 
   adView.loadAd(adRequest);
}


}


Step 4 :  Add this Code in your activity_main  xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:background="@android:color/darker_gray"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="a15264bfhdbd4fsd"
                         android:layout_gravity="center_horizontal|center_vertical"
                         
                         ads:adSize="BANNER"/>
</LinearLayout>

NOTE : Change your adunitId  in xml file  and replace to this one (a15264bfhdbd4fsd)


Step 5 : Add this code in your manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ritesh.admobdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <meta-data android:name="com.google.android.gms.version" android:value="4030500" />
        <activity
            android:name="com.ritesh.admobdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name="com.google.android.gms.ads.AdActivity" 
   android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>

</manifest>

Run The Project







1 comment:

  1. Very useful code ..now its easy to implement admob .. thanks :)

    ReplyDelete