How to add Android AdMob Ads using Google Play Services

admob android google play services

We will work with already existing project using Eclipse IDE. Google has already made good tutorial about how to add AdMob in Android Studio, and I will tell you an alternative option.

I took the project from previous tutorial about ContentProvider, its xml layout file and Activity code you can check here.

First of all, we need to download (or check that you have it) the last version of Google Play Services package:
1. Open Android SDK Manager (Window -> Android SDK Manager)
2. Scroll it down to the Extras section and check if there is an update or we already have the latest version.

admob android google play services



Now let’s import Google Play Services library, so that we could link it to our project:

1. File -> Import… -> Existing Android Code into Workspace
2. Library files are in the Android SDK folder: \sdk\extras\google\google_play_services\libproject\google-play-services_lib
3. Choose this folder and click Finish.

Now we only need to link this library to the project:

1. Open our project’s properties: select file with the project in Eclipse, File -> Properties
2. In the Properties window open Android section, and in the Library table below click Add…
3. In the new window choose google-play-services_lib – and that’s it, library is linked.

Now we need to change AndroidManifest.xml file.
Admob requires internet connection and ability to check current network connection, so we need to add following permissions (declare it in <manifest> tag):

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

For AdMob to work correctly we should set proper versiob of Google Play Services library and define ad Activity (declare it in <application> tag):

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

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

Now let’s move to xml layout. Previously it looked like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvItems"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

We will place Admob ad under the listview using this element:

<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="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

And out activity_main.xml should look as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvItems"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </ListView>
    
    <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="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</LinearLayout>

!!! Don’t forget to add namespace

xmlns:ads="http://schemas.android.com/apk/res-auto" 

It prevents from errors with ads:adSize and ads:adUnitId.

Ad unit ID you can add in /res/values/strings.xml as follows:

<string name="banner_ad_unit_id">ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx</string>

Don’r forget to insert real Admob identificator! You can find it at Admob website, in App Management section, after clicking on Ad units.

Now let’s make our ad loading by changing onCreate() method of our MainActivity class:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	//....
	AdView mAdView = (AdView) findViewById(R.id.adView);
	AdRequest adRequest = new AdRequest.Builder().build();
	mAdView.loadAd(adRequest);
	//....
}

And launch the app:

admob android google play services

5 Responses to “How to add Android AdMob Ads using Google Play Services”

  1. Ardi says:

    I am using latest google play services sdk and this way does not work anymore. can you make post about it ?

    thanks

Leave a Reply

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