Jimmy's Blog – Adding AdMob's ads to Android Application

Finally I got some spare time to make new article on my blog :)

One of the easy way to monetize our Android application is by adding ads.
This tutorial will give basic explanation  about how to add AdMob ads to Android Application.

Prerequisite:


Create Android Project

I create a simple android project with 1 activity and 1 layout.

AdMob library

Copy AdMob lib file (GoogleAdMobAdsSdk-6.0.1.jar) to your lib folder, so Eclipse will automatically include it to your build.
And also add the library to project’s build path.

Package Explorer

Permissions and AdMob Activity

There’s 2 things that we need to add to ‘AndroidManifest.xml’

  • Permissions for ‘android.permission.INTERNET’ and ‘android.permission.ACCESS_NETWORK_STATE’
  • AdMob ‘com.google.ads.AdActivity’ Activity for handling configuration changes.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gnu.jimmod.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".AdMobActivity"
            android:label="@string/title_activity_ad_mob" >
            <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>

Adding Ads to Layout XML

First way to add AdMob ad is by adding AdView to your XML layout.
Parameter that need to be specified:

  • ads:adUnitId : your AdMob app publisher ID
  • ads:adSize : ads size
  • ads:testDevices : list of testing devices (so only displaying dummy ads) – below there will be explanation how to know your device ID.
  • ads:loadAdOnCreate : boolean – ads will be automatically requested on layout displayed

activity_admob.xml

<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:orientation="vertical"
    >
 
    <textView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".AdMobActivity" />
 
    <com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_gravity="center"
                         ads:adUnitId="a14ff402be4457c"
                         ads:adSize="BANNER"
                         ads:testDevices="TEST_EMULATOR"
                         ads:loadAdOnCreate="true"/>
 
</linearLayout>

Admob Publisher ID

Adding Ads via code

Adding AdView at runtime is as simple as adding other Views. Same like adding in XML you will need to specify the publisher-ID, ads size and test device list.

AdMobActivity.java

package gnu.jimmod.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
 
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
 
public class AdMobActivity extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admob);
 
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
 
	    AdView adView = new AdView(this, AdSize.BANNER, "a14ff402be4457c");
	    layout.addView(adView);
 
	    AdRequest adRequest = new AdRequest();
	    adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
		adView.loadAd(adRequest);
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_admob, menu);
        return true;
    }
 
 
}

How to get my phone device-ID

If you want to add your phone as test device, you will need to run your project with your phone via USB.
Once you run it on your phone, there will be log in Eclipse LogCat window about your device-ID

device_idThen you can copy and paste it to your code.

Screenshot

Screenshot

Summary

Basically adding ads to your application is simple. We just need to be creative on how and when displaying the ads.
I’m sure I will write more about ads on android, especially on ways to display ads on our apps.

Until then..
Thank you for reading, hope this article able to help you.

Source Code

Download the tutorial source code here:
https://bitbucket.org/jimmod/blog_android_admob/changeset/53f720effced

10 comments

  1. @euler:
    For netbeans you can use the same code.

    @Galih:
    Bikin APK kalo pake Eclipse tinggal klik kanan di project – android tools – export signed package

  2. makasih mas artikelnya sangat membantu..
    untuk akun yang udah diupgrade ke versi terbaru admob

    bisa langsung pake adunit yg formatnya :
    ca-app-pub-xxxxxxxxxx/nnnnnnnnnnnnn”

  3. This post is very informative but it took me a long time to find
    it in google. I found it on 19 spot, you should focus on quality backlinks building, it will help you to rank to google top 10.
    And i know how to help you, just search in google – k2 seo tips and tricks

  4. I see a lot of interesting posts on your blog.

    You have to spend a lot of time writing, i know how to
    save you a lot of work, there is a tool that creates high quality, SEO friendly posts in couple of
    minutes, just search in google – k2 unlimited content

  5. this code is not working sir.
    I am trying a lot to do.
    This code worked for me in android4.4
    but now I deleted it and installed android5.01
    If an application is created in android 5.01 or android 5.0 then it is telling “UNFORTUNATELY STOPPED”
    can you pls help me to get ads in android 5.01 and android5.0
    I ma trying many ways and from 15 days
    pls help me

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP-SpamFree by Pole Position Marketing