Tuesday, April 1, 2014

How to add Google AdSense in Android Application

,
In this android tutorial, we will learn how to show Google Ads on Android Application. We will need the following two things: 
1. Ad Unit Id from Admob.com
2. Google Play Services installed in Android SDK.

 

How to get Ad Unit Id from admob.com:

1. Go to admob.com
2. Create New Account (if you are already an admob user, then log in).
3. Select Sites & Apps.
4. Select the Device like Android/iOS/Windows.
5. Fill The Description of Your Application -> OK.
6. Go to Sites & Apps.

How to add Google AdSense in Android Application
admob.com


7. Select Manage Application.

How to add Google AdSense in Android Application
Sites & Apps -> Manage Settings

8. Now you can see Ad Unit Id.

How to add Google AdSense in Android Application
Get Ad Unit Id(Publisher ID)

How to get Google Play Services Folder: 

 

1. Open android SDK -> Click on Windows -> android SDK manager -> extras -> download Google play services.
How to add Google AdSense in Android Application
Install Google Play Service Packages


Now we have Admob Ad Unit Id and Google play services folder. Create a new project to test Ads on android Application and use Google play services as a library in your android project.

 

How to Add Google Adsense code in Android Application:


1. Now select project -> right click on project -> properties -> android -> add library -> select Google play services folder -> Apply.

How to add Google AdSense in Android Application
Add Google Play Services Package As a Library

2. Open your android XML file and add the following line in layout:   
           xmlns:ads=http://schemas.android.com/apk/res-auto
3. 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"/> 
4. The Android XML file will look like this:


<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="Your_AD_UNIT_ID"/>
</RelativeLayout>
5. Now open your Java file and initialize AdView Object in your Activity.

                 AdView ad=(AdView)findViewById(R.id.adView);
6. Load the ad from Google:

                 ad.loadAd(new AdRequest.Builder().build());

Now change AndroidManifest.xml file according to the code given in the end of the tutorial & run your project. Now install the application on your phone & check ads!

For Interstitial Ads: This Ad will be displayed on your phone screen.

 

1. Use the following code in your activity: 

    InterstitialAd end_ad=newInterstitialAd(this);
    end_ad.setAdUnitId(getResources().getString(“Your_AD_UNIT_ID”));
    end_ad.loadAd(newAdRequest.Builder().build());

2. Show Ad if loaded:

if(end_ad.isLoaded())
          {
             end_ad.show();
          }

Now change AndroidManifest.xml file according to the code given in the end of the tutorial & run your project. Now install the application on your phone & check ads!

Now change AndroidManifest.xml file:

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Main_Activity"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18"/>

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

<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>

<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.example.Main_Activity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid: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>

Both the above examples are used in the following Android applications:

JMI SPI Converter

Namaz Guide in English

Download and test the APK File.
Please share and comment to improve this blog.
For more Android tutorials see List of Android Tutorials. Thanks to all.. :)

0 comments to “How to add Google AdSense in Android Application”

Post a Comment

 

Download Android Game Copyright © 2016 -- Powered by Blogger