Friday, August 21, 2015

Integrating Google AdMob Ads in Android Application

I was wondering how to integrate Advertises from google in my Android applications. AdMob is the best mobile advertising site which you can integrate with your Android Application very easily.
In this blog, you learn how to show AndMob Advertises in your Android Application.  This write up is divided into below three parts: 

1.     Getting Ad Unit Id from Admob Site
2.     Installing Google Play Services in Android SDK
3.     Integrating AdSense code to Android App.

1)     Getting Ad Unit Id from Admob Site :

1.     Go to AdMob Site

2.     Create New Account (if you are already an admob user, then log in)

3.     Select "Monetize new app" option.


4.   Search your android application for which you want to integrate the Admob. You can search it in Google Play or you can add the details about the application.



       5. After filling all information, you get the instructions to set up admob unit id.


6. Now, your app is added to the admob site. You can check this in all apps list in Monetize tab of        this site. You can select this app and create the ad banner and get the ad unit id which looks as:
ca-app-pub-50xxxx69xxxxxxxx/34xxxxxxxx

7.  You need to use this Ad Unit Id in our project later.


2)     Installing Google Play Services in Android SDK


1.     Open the Eclipse and Go to Windows->SDK Manager :



2.  In the packages select Extras->Check Google Play Services and install it as shown below: 


3.    Import the Google play Services to you Workspace.

4.   Now Go to your project where you want to integrate this admob, and add this Google play services library to this project: 




3)     Integrating AdSense code to Android App:

     1.  Open your android XML file and add the following line in layout:

xmlns:ads=http://schemas.android.com/apk/res-auto      
  
2.     Add AdView in Layout:

<com.google.android.gms.ads.AdView
 android:id="@+id/adView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 ads:adSize="BANNER"
 ads:adUnitId="YOUR_AD_UNIT_ID"

/>

3.    Now open your Java file and initialize AdView Object in your Activity:
AdView ad=(AdView)findViewById(R.id.adView);
     4.   Load Ad from Google:
ad.loadAd(new AdRequest.Builder().build());
5.  Now modify the AndroidManifest.xml:

      a.      Add Permissions: 

<!-- Used to request banner ads. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Used to avoid sending an ad request if there is no connectivity. -->

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

     b.     Need to add metadata of Google play Services: 

<meta-data
 android:name="com.google.android.gms.version"

 android:value="@integer/google_play_services_version" />

           
     c.    Need to add the below activity to show the ad overlays:

<activity
 android:name="com.google.android.gms.ads.AdActivity"

  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

  d.      Final AndroidManifest.xml looks as below:
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.marutsoft.sudoku"
      android:versionCode="3"
      android:versionName="1.3">
    <uses-sdk android:minSdkVersion="7"
        android:targetSdkVersion="19"/>
    <!-- Used to request banner ads. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <meta-data
 android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version" />

         <activity android:name="com.marutsoft.sudoku.Splash"
                  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 required to show ad overlays. -->
<activity
 android:name="com.google.android.gms.ads.AdActivity"
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
            
    </application>

</manifest>
   
Now run your application and check that google adverise is shown in the activity where you have added it. Please note you need to have the internet connectivity.

Also you can add the code to check the availability of the connectivity and then load the advertise. We will add that part in my next blog

That’s it. Happy Coding..






No comments: