Sunday, March 10, 2013

Ad-sense Integration in Android : Display add into Android Application


Hello friends,
    Today I am going to share code for Ad-sense integration in Android. Follow my blog step by step-
Screen-shot:

1) First Download AdWhirlSdk.jar and GoogleAdMobAdsSDK.jar and add it into your project using build path.

2) Add Internet permission in manifest.xml
<uses-permission android:name="android.permission.INTERNET" />

3) Add ads activity in manifest.xml
<activity
 android:name="com.google.ads.AdActivity"         android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|uiMode|screenSize|smallestScreenSize" />

4) Use frame layout to display add anywhere in your layout.

  <FrameLayout
        android:id="@+id/adds_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.adwhirl.AdWhirlLayout
            android:id="@+id/adwhirl_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

5) Put your Ad-sense key in your Activity class.
Addlayout = (FrameLayout) findViewById(R.id.adds_layout);
              AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this,
                           "put Ad-sense key here!");
              Addlayout.addView(adWhirlLayout);

My  Code

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.addsensedemo.AddSense"
             android:screenOrientation="portrait"
            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="orientation|keyboard|keyboardHidden|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>

</manifest>

2)AddSense.class
package com.example.addsensedemo;

import com.adwhirl.AdWhirlLayout;

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

public class AddSense extends Activity {
       FrameLayout Addlayout;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_add_sense);
              Addlayout = (FrameLayout) findViewById(R.id.adds_layout);
              AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this,
                           "Paste Ad-Sense key here!");
              Addlayout.addView(adWhirlLayout);
       }


}

3)activity_add_sense.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddSense" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textSize="25sp" />

    <FrameLayout
        android:id="@+id/adds_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <com.adwhirl.AdWhirlLayout
            android:id="@+id/adwhirl_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

</RelativeLayout>


For more help comment on my blog!
Thanks,


4 comments:

  1. can u send me Zip File of this code?

    ReplyDelete
  2. How to create an adsense key?
    pls Help me. I am new to it.

    ReplyDelete
    Replies
    1. do googling!
      Well its provided by google so request to them for your application..

      Thanks,

      Delete
  3. Hi Manish,

    I heard while people talking that if we use adsense key in our android apps then Google will suspend our Adsense account. Is it right.?

    ReplyDelete