Saturday, 10 August 2013

Android Admob Sample Code

Leave a Comment
Today we have an interesting topic to cover that will help many android developer who have their free android application on Google Play. The Google Admob Ads SDK is the latest generation in Google mobile advertising having well refined ads for mobile.
Lets begin with sample code demo:-
Step 1:- Get Registered with Admob to get the publisher Id
After filling basic information Click –> Add Site/App
and fill the information Like below.
registration



Step 2:- Download the SDK
download sdk


Step 3:- Get the Publisher ID, hover over added site/App and click on Manage setting where you Get the publisher ID.
publisherId


Step 4:- Create a sample demo project named, AdmobSample.Add GoogleAdMobAdsSdk.jar file into libs folder of project directory.


Step 5:- Android Manifest
Add an activity and permission into manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.arpit.admobsample"
    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" >
        <activity
            android:name="com.arpit.admobsample.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.ads.AdActivity"
          android:configChanges="keyboard
|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>
</manifest>


Step 6:- Let me tell you one more thing
 you can add Ads in your application in two ways :-
1.In Xml
2.In Java file
Either Define Ads  in xml or in Java.



Let us get started with the xml method.
The highlighted code is a way to add Ads in your Xml. There is no need to do anything in java file now.

<?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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.google.ads.AdView
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="YOUR_PUBLISHER_ID"
        ads:loadAdOnCreate="true" />   //load code while onCreate calls

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>

Second way is to define Ads in java source file
package com.arpit.admobsample;
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 MainActivity extends Activity {
    LinearLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
// this is code to add Ads in javafile. Took a layout and define add in it.
    layout = (LinearLayout) findViewById(R.id.linear);
  AdView ad = new AdView(MainActivity.this, AdSize.BANNER, "YOUR_PUBLISHER_ID");
        layout.addView(ad);
        ad.loadAd(new AdRequest());
    }
   
}



Result:- Google Play Ad
result