Friday, August 21, 2015

Integrating Google AdMob Banner Ads in Android (Step‑by‑Step)

I was wondering how to integrate Ads from Google in my Android applications. AdMob is a widely used mobile advertising platform that you can integrate with your Android app fairly easily.

In this post you will learn how to show AdMob banner ads in your Android application. This write‑up is divided into three parts:

  • Getting an Ad Unit ID from the AdMob site
  • Installing Google Play Services in the Android SDK (Eclipse instructions as originally used)
  • Integrating AdSense/AdMob code into your Android app

Note: this article preserves the original Eclipse/SDK Manager workflow used when it was written. Most developers today should use Android Studio and the Google Mobile Ads SDK via Gradle. See the “Modern tooling” note at the end for guidance.

1) Getting Ad Unit Id from the AdMob site

  1. Go to the AdMob site: https://www.google.com/admob/
  2. Create a new account (or log in if you already have one).
  3. Select the “Monetize new app” option.
  4. Search your Android application (from Google Play) or add the app details manually.

AdMob step 1

  1. After filling the app information, AdMob shows setup instructions and lets you create an ad unit and get the Ad Unit ID.

AdMob select app

  1. Your app will be added in AdMob. In the Monetize tab you can select the app, create a banner ad, and copy the Ad Unit ID (it looks like):
ca-app-pub-50xxxx69xxxxxxxx/34xxxxxxxx
  1. You will use this Ad Unit ID in your project later.

2) Installing Google Play Services in Android SDK (Eclipse era instructions)

These steps describe the older Eclipse + SDK Manager workflow used when this post was written.

  1. Open Eclipse and go to Window -> SDK Manager.

SDK Manager

  1. In the packages list select Extras -> check Google Play Services and install it.

Install Google Play Services

  1. Import the Google Play Services project/library into your workspace.
  2. Add the Google Play Services library to the project where you want to integrate AdMob.

Add library to project

3) Integrating AdSense / AdMob code into the Android app

  1. Open your layout XML and add the Ads XML namespace (required to use ads: attributes):
xmlns:ads="http://schemas.android.com/apk/res-auto"
  1. Add an AdView to the layout. Example:
<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" />
  1. In your Activity Java file initialize the AdView object:
AdView ad = (AdView) findViewById(R.id.adView);
  1. Load an ad from Google:
ad.loadAd(new AdRequest.Builder().build());
  1. Modify AndroidManifest.xml: add required permissions, metadata for Google Play Services, and the AdActivity entry.

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" />

Add Google Play Services metadata (inside ):

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

Add the AdActivity which is required to show ad overlays:

<activity android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

The original article includes a sample manifest; here is the final manifest excerpt reproduced for reference:

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 the Google ad is shown in the Activity where you have added it. Please note you need to have internet connectivity.

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

That’s it. Happy Coding..


Modern tooling note (recommended)

This article preserves the original Eclipse instructions. If you are using Android Studio (recommended):

  • Use the Google Mobile Ads SDK via Gradle (implementation 'com.google.android.gms:play-services-ads:…') and follow current AdMob docs.
  • Newer SDKs require an application-level AdMob App ID meta‑data entry in AndroidManifest.xml and explicit MobileAds.initialize(…) in your application code before loading ads. Also always use test ads or register test device IDs while developing.

If you want, I can provide a modern, Android Studio + Gradle version of these steps with updated code samples and a short checklist for publishing with AdMob.

No comments: