How to Add AdMob to Android [Deprecated]

NOTE 1: This tutorial is now deprecated. See this one instead!

NOTE 2:  This project is made in Intellij. For those who use Eclipse or other IDE the integration of AdMob library will differ.

You can use AdMob lib only on at least Android v3.2 (Api Level 13), so you must compile your project on this version (at least). But if you want your application to work on older version set in your AndroidManifest.xml

<uses-sdk android:minSdkVersion="7"/>

1. First you have to download the AdMob SDK library from here
2. Now go to where you have downloaded the library and copy the jar (inside the zip) file GoogleAdMobAdsSdk-6.0.1 and paste it in the libs folder from Intellij

3. Now you have to install the jar. To do this you must go to the Project Structure in Intellij (the shortcut CTRL + ALT + SHIFT + S). Here go to Modules – Dependencies and click on the “+” button from the right and select Library – Java. It will open a window and you will have to select the .jar from libs from your project. Click OK on all windows that open.

4. Now create a new project called AdMobExample with the main class called MyActivity. Now go to the AndroidManifest.xml and introduce the following code just before the application tag closes.

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

5. We must put the Internet permissions in the manifest file just before manifest tag closes.

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

6.Now go to the res – layout – main.xml and put an id to the layout called main_layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main_layout">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World, MyActivity"/>
</LinearLayout>

7. At this last step you have to go to MyActivity class and introduce the following code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MyActivity extends Activity

{
    AdView adView;
    public static final  String MY_PUBLISHER_ID = "here you must put your publisher id";


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //request TEST ads to avoid being disabled for clicking your own ads
        AdRequest adRequest = new AdRequest();

        //test mode on EMULATOR
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

        //test mode on DEVICE (this example code must be replaced with your device uniquq ID)
        adRequest.addTestDevice("4G74FC73D62D42B62A7F7DA61EF5F776");

        //create a Banner Ad
        adView = new AdView(this, AdSize.BANNER, MY_PUBLISHER_ID);

        //call the main layout from xml
        LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout);

        //add the Banner Ad to our main layout
        mainLayout.addView(adView);

        // Initiate a request to load an ad in TEST mode. The test mode will work only on emulators and your specific test device, the users will get real ads.
        adView.loadAd(adRequest);


    }


    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
}



NOTE: To get your device ID you have to run  your application on the DEVICE in test mode and wait for an ad to load.  Then go to logcat output and search for the ID.  It should be at Log INFO like in the picture below. You can add as many devices as you want.

Maybe you wonder how to obtain the AdMob ID publisher. Don’t worry, I will explain this too.
1. You have to go to AdMob site and log in with your google account or you can create an AdMob account.
2. After you have logged in, go to Sites & Apps and click on Add Site/App:

3. Now you must introduce your application details or you can use the details from the below picture for testing.

4. After you click continue it should see a screen with your Publisher ID. But you can find it too if you click on Add Sites/Apps

5. Down on this page you should find the application you just added. To get to the Publisher ID click on the Manage Settings button.

6.Now you can get your Publisher ID

 

AND this should be the result of our tutorial:

References: https://developers.google.com/mobile-ads-sdk/docs/android/fundamentals
https://developers.google.com/mobile-ads-sdk/docs/bestpractices#testmode
http://www.basic4ppc.com

Menu