Saturday, 3 January 2015

AdMob tutorial


In this tutorial, you will learn how to integrate the new Google Admob with banner and interstitial ads into your Android application. The new AdMob is a streamlined user interface, to make it even easier for you to monetize and promote your apps in minutes. Maximize your earnings with the new AdMob improved tools to help app developers build their business. We will create a simple app where we will have an banner ad and Interstitial ad.So lets begin…

So at end of this post you will find some live ad shown in your layout in the following picture.

If you're a developer and you're looking for a way to make money on your own app or game while still keeping it free, you can make decent money using the AdMob system.


            
Live Banner Ad.


Step 1:

Sign up as an Admob Publisher here. Log in to your dashboard once you have your account approved.

Once you have created your account you can find your publisher id at top right of your dashboard.


                       
Now click on  Monetize tab and create an ad unit for your application. You should be able to see your ad unit after selecting an app on your left panel.                                                             
step-1(a)
                                                       
step-1(b)


                             
step-1(c)

step-1(d)

You will find  the Ad Unit Id (The unique identifier for an ad unit) which is shown in step-1(d) please not this id somewhere which will be used in your application to get some demo or live ad's which will be discussed in the our next step.

Step 2:

Download the new Google Play Services Library using the Android SDK Manager in your Eclipse IDE.
        SDKManager

once the Google play services got installed you will find an google play services libproject  in  your sdk.
In my case I found the project in the following location:
F:\android_eclipse\adt-bundle-window\sdk\extras\google\google_play_services\libproject.

Now import the above project into your workspace.


Step 3:






Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project DemoAd.
   Application Name : DemoAd
   Project Name : DemoAd
   Package Name : com.example.demoad
    Import Google Play Services Library into your App.

Right click on the project that you have just created now you will find the window being open.
   

  
Now this 
google_play_services_lib.jar is added into your project.

Step 4:

Now add the lines highlighted in blue color in your Manifest.xml:
-------------------------------------------------------------------------------------------------------------

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        
    //add the above two lines before <application> tag     

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <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"
            android:theme="@android:style/Theme.Translucent" />
        
        <activity
            android:name="com.example.demoad.activity.DemoActivity"
            android:label="@string/app_name" 
            android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Now add the lines highlighted in blue color in your activity_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/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Portrait Orientation Phone Layout"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <com.google.android.gms.ads.AdView
        android:layout_marginTop="10sp"
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxxxxx" >
    </com.google.android.gms.ads.AdView>

     //place a  linear layout having edit text and text view as shown in the 
    //beginning of the post

</LinearLayout >

Note: Replace ads:adUnitId="ca-app-pub-xxx/xxxx"  with your id generated in step-1(d).


Open your DemoActivity.java and paste the following code.
-------------------------------------------------------------------------------------------------------------------

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.demoad.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class DemoActivity extends Activity {
          
         private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

// Get the view from activity_main.xml
setContentView(R.layout.activity_main.xml);
                 
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) findViewById(R.id.adView);
                 
                  //Request for Ads and Add a test device to show Test Ads
AdRequest adRequest = new AdRequest.Builder()
                   
                 //Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .addTestDevice("CC5F2C72DF2B356BBF0DA198").build();
                  // Load ads into Banner Ads
adView.loadAd(adRequest);

}//end of onCreate()
}//end of class

In this DemoActivity.java, a test device is being used to run test ads. Its highly recommended to test your ads using test ads to prevent invalid clicks that will cause suspension of your AdMob publisher account. To generate a Test Device ID, simply type in some random text intoaddTestDevice as shown below.

       // Request for Ads

AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("abc") //Random Text
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
So by following all the above 4 steps you will able to get some demo ad's in your android 
application.
The final output look like this you can find some live ad being displayed on the top.
Live Banner Ad.
still there are some more topics left like loading  live ad's,interstitial ad's,different types of banner ad's and integrating some 3rd party video ad's etc... which will be discussed in my upcoming posts....

No comments:

Post a Comment