Sunday, 21 September 2014

How to Integrate AdMob in Android Application

What is Admob?
AdMob is the Google product which is featured with display advertisement on mobile Apps by integrating with applications like Google AdSense for websites. It's not only for Android apps also featured for iOS and Windows Phone 8.

For more details follow below link.
Guide for AdMobe

For android go through Android Quick Start .
Develop using Eclipse IDE follow https://developer.android.com/training/monetization/ads-and-ux.html

Develop using Android Studio follow https://developer.android.com/training/monetization/ads-and-ux.html

To integrate AdMob in android applications first of all you need to update GooglePlayService through follow below steps.
Step-1:
Open your Eclipse IDE Window->open "Android SDK Manager"-> Extras and install Google Play Services

Step-2:
import "google play service"  which will be in android SDK (android-sdk-windows\extras\google\google_play_services).

Step-3:
 Integrate android AdMob SDK using "add external JAR" Right Click on project click on properties->Java Build Path->Libraries->click on "Add External JARs" using this path "android-sdk-windows\extras\google\admob_ads_sdk".







activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   
    <TextView
        android:id="@+id/txtapp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:textStyle="bold"
        android:text="Test app to integrate Banner AdMob with own App "/>
  

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        android:layout_alignParentBottom="true"
        ads:adUnitId="ca-app-pub-123456789/123456789" />

</RelativeLayout>

MainActivity.java

package com.rakesht.admobapp;

import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends Activity  {

  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);
      

               //Locate the Banner Ad in activity_main.xml
        AdView adView = (AdView) this.findViewById(R.id.adView);

        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder()

        // Add a test device to show Test Ads
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
         .addTestDevice("45F6EA8B4B1C1EBE87E350EC70CAA02D")
                .build();

        // Load ads into Banner Ads
        adView.loadAd(adRequest);


            }
   
  }

AdMobApp Manifest.xml


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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
 <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" >
        <activity
            android:name="com.rakesht.admobapp.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>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

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

</manifest>

Output :