Sunday, June 10, 2012

How to add Google ads in Android apps (Google Admob Ads)

These day you can find many Android app are having great features you know what still they are FREE available in Android Market/Google Play why?

Because many of those earning money through Google Ad-mob Ads showing in the apps.

so the question is how we can integrated these Google or Any ads into our Android Apps.

I am going to write this page to achieve this....

Before starting codding stuff we need  Google Advertisement Publisher ID.

Get Ads Publisher ID

First of all you need to sign-up account at Google AdSense
if you don't have then fill-up some bank and transaction stuff you will get you client ID

eg. Mine is "a1500db2724b9b8"

okay now let's create a small app with...
 -------------------------------------------
App Name: GoogleAdMob
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 /  API 10
Default Activity Name: ActivityGoogleAdMob
-------------------------------------------


Download and Add Google AdMob SDK

You can download latest Google AdMob Ads SDK here
now we need to add this sdk into our GoogleAdMob app

So right click on project --> properties-->
java build path -->Libraries-->Add External JARs see below image




Now start some coding stuff this way... your main activity should be..

ActivityGoogleAdMob.java

package com.rdc;

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 ActivityGoogleAdMob extends Activity {
private AdView adView = null;
private static final String ADMOB_ID = "a1500db2724b9b8";

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

// Create the adView
adView = new AdView(this, AdSize.BANNER, ADMOB_ID);

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

// Add the adView to it
lay.addView(adView);

// 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();
}

}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


Manifest file will be
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

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

<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ActivityGoogleAdMob"
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>
</manifest>

Note: you must have app target is API 13 or newer
see the file "default.properties"
# 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,
# "build.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-13

The output Screen will be like this..



You can download the Google AdMobSDK 6.0 and complete source code zip file here : GoogleAdMob

cheers!!

 I'd love to hear your thoughts!

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home