Monday 16 April 2012

Integrate AdMob in Android

How to Integrate AdMob in android device??

First of all open project.Properties file and set target to 15

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "ant.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-15


Now download GoogleAdMobAdsSdk-4.3.1.jar file and copy & paste it into your project.
Then Select your project--> Right Click-- > Properties --> Java Build path --> Libraries --> Add JARs --> select your project from list and select that GoogleAdMobAdsSdk-4.3.1.jar fiel --> ok


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/lib/com.google.ads"
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

   
    <LinearLayout
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/mainLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>


In AndroidMenifest.xml file Provide Internet and Network_state permission and add com.google.ads.AdActivity activity (See following code)


  <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AdvertisementActivity"
            android:configChanges="keyboardHidden|orientation"
            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.ads.AdActivity"          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>

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


In AdvertisementActivity.java (Main Activity)

package com.advetise.ads;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class AdvertisementActivity extends Activity {
    /** Called when the activity is first created. */
   
    private AdView adView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DisplayAds();
    }

    public void DisplayAds() {
        // Create the adView
        adView = new AdView(this, AdSize.BANNER, "YOUR_KEY_HERE");
        // adView .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        // LayoutParams.WRAP_CONTENT));

        // Lookup your LinearLayout assuming it’s been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);

        // Add the adView to it
        layout.addView(adView);
        AdRequest re = new AdRequest();
        re.setTesting(false);// this must be false when apps is released in the
                                // market
        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

    }

    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

}

Thanks a lot...
Ronak Pandya....

No comments: