Implementing Admob Ads Into Your Android App

In this tutorial, you will learn how to implement Google Admob Ads into your Android application. Admob Ads enables mobile application developers to maximize their monetization on Android by tapping into Google and AdMob’s vast combined pool of advertisers. In other words, this a way to make money with your Android Application. We will create a simple view that shows an Admob test ad in an XML graphical layout. So lets begin…

[alert-announce] This tutorial refers to the legacy Admob and may differ from the New Admob that have been launched on May 2013. [/alert-announce]

GETTING STARTED

Sign up as an Admob Publisher here and download the Google Admob Ads SDK. Log in to your dashboard that shows your revenues and estimated earnings.

To create an ad for your application, click on the Add Site/App Button.

Admob Add Site or App

Fill in the required information. The Android Package URL is the package name for your application. Locate your package name in your AndroidManifest.xml.

To locate your Publisher ID, click on Manage Settings shown on the screenshot below.

Admob Manage Settings

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project AdmobTutorial.

Application Name : AdmobTutorial

Project Name : AdmobTutorial

Package Name : com.androidbegin.admobtutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.admobtutorial;

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from activity_main.xml
		setContentView(R.layout.activity_main);
	}
}

Extract the downloaded Google Admob Ads SDK and insert into your libs folder.

Google SDK Libs

Next, insert GoogleAdMobAdsSdk-6.2.1.jar to your build path. To do that, right click on GoogleAdMobAdsSdk-6.2.1.jar > Build Path > Add to Build Path.

Next, create an XML graphical layout for your MainActivity. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

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/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="MY_AD_UNIT_ID"
        ads:loadAdOnCreate="true"
        ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />

</LinearLayout>

Insert in your Publisher ID into “MY_AD_UNIT_ID” and your TEST_DEVICE_ID.

Example

<com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="abc1234567890"
        ads:loadAdOnCreate="true"
        ads:testDevices="TEST_EMULATOR, 1234567890" />

LOCATE DEVICE TEST ID

Run your application and in your LogCat and type “ads” in the textbox provided to filter the errors. Locate the test device ID shown on the screenshot below.

Admob Test Device

In your AndroidManifest.xml, we need to declare an activity for Google ads and permissions to allow the application to access to the Internet and check network status. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.admobtutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <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>

Output:

Admob Tutorial ScreenShot

Source Code 

[purchase_link id=”7850″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

Премного благодарен)

Alex

Implementing Admob Ads Into Your Android App