Android SDK – Adding AdMob

image

 

There are two ways for adding AdMob ads to your apps. The most common way is with the SDK provided by AdMob when you add an app, and the other way is with an alternate AdMob SDK provided by Google. In this tutorial, I will show how you can quickly setup ads in your app using the alternate AdMob SDK. Other then the difference in how the SDK works, I’m not sure what the differences between these are.

The official site can be found here: http://code.google.com/mobile/ads/docs/android/fundamentals.html

I find it interesting that every time I look into working with some SDK, the instructions and sample app that is provided never work. There always seems to be something missing. I don’t know if they simply don’t test when they write these docs, or what. Regardless, I have plugged the whole and will show you every detail so that you can get this working.

One thing I’d like to point out, is that what I demonstrate is the simplest ad functionality, which is enough in most cases. If you really want to dig in, you can get fancy with tracking and even serving ads based on various criteria, such as gender and location. They also have a feature called “Interstitial Ad”, which is defined by Google as…

Interstitials present rich HTML5 experiences or “web apps” at natural app transition points such as launch, video pre-roll or game level load. Web apps are in-app browsing experiences with a simple close button rather than any navigation bar—the content provides its own internal navigation scheme.

The richer, more immersive nature of these ads makes them more expensive and subject to impression constraints.

However, it’s apparently only open to select publishers…

Interstitial ads are currently only offered to a select group of publishers. If you become eligible, Google will be sure to contact you.

Anyway, back to the topic. If you break it down, there are really only four steps that need to be done to get this to work.

  1. Import the .jar into your project
  2. Setup the layout xml
  3. Update AndroidManifest.xml
  4. Create and request ads in the project .java file

I use Eclipse, so any reference to a development platform will be for that.

Step 1: Import the .jar into your project

Create your Android project as usual and download the sample project HERE. The reason you need the sample project is because it contains the required GoogleAdMobAdsSDK-4.0.2.jar file. After unzip the sample project, drag the libs folder onto the top level of your project in Eclipse. This will copy it and the jar into your project. Finally, link the jar file to your project by right-clicking the top level folder and clicking properties.

image

Select “Java Build Path” and click the Libraries tab. Now just click the button “Add JARs”. This will bring up another window. Expand your project and the libs folder, then select the jar file. Click Ok, click Ok again and you’re done with step 1.

Step 2: Setup the layout xml

Expand the res—>layout folder and double-click main.xml to open the edit view. If it opens in the “Graphical Layout”, just click the tab on the right, “main.xml”. If you renamed this layout file, that name will be reflected here. The default view already has everything you need, except for a LinearLayout id. Add the following line.

android:id="@+id/linearLayout"

The final xml should look like this.

"1.0" encoding="utf-8"?>

"http://schemas.android.com/apk/res/android"

    android:id="@+id/linearLayout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">


    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />


Step 3: Update AndroidManifest.xml

Directly above the tag, add the following.

"com.google.ads.AdActivity"

              android:configChanges="keyboard|keyboardHidden|orientation" /

And above the tag…

"android.permission.INTERNET"/>

"android.permission.ACCESS_NETWORK_STATE"/>

Step 4: Create and request ads in the project .java file

According to the instructions and sample app provided, the following code needs to be added in the onCreate activity after the line, setContentView(R.layout.main); and replace “MY_AD_UNIT_ID” with your AdMob publisher ID.

// Create the adView

AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);


// Lookup your LinearLayout assuming it’s been given

// the attribute android:id="@+id/linearLayout"

LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);


// Add the adView to it

layout.addView(adView);


// Initiate a generic request to load it with an ad

adView.loadAd(new AdRequest());

However, when you try this, you get an error with the number you enter for the publisher ID. It tells you that the variable has not been declared. For some reason, the function is not accepting a string passed directly into it. So to fix this, you need to add the following line above, “AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);”

String pubID = "YOUR_PUBLISHER_ID";

Replace “YOUR_PUBLISHER_ID” with your publisher ID. What this does is create a variable called “pubID”, and sets it to equal whatever you enter as the publisher ID. Now back to the line where it originally requested your ID and replace “MY_AD_UNIT_ID”, with this new variable, “pubID”.

AdView adView = new AdView(this, AdSize.BANNER, pubID);

It works when done this way.

Finally, add these imports at the top, if they have not already been added.

import com.google.ads.AdRequest;

import com.google.ads.AdSize;

import com.google.ads.AdView;

import android.widget.LinearLayout;

If you run this project, you will start sending requests for ads. While in development, you should enable test mode so that you will get ads every time. When it’s live, you won’t always get an ad, so to make sure it’s setup correctly and if you want to play around with different settings, enable test mode. To do this, add the following before “adView.loadAd(request);”

request.setTesting(true);

Ok, run the project and when it starts, you should see an ad. I did notice that even in test mode, the first time you run it, it doesn’t return an ad every time. If you exit, and run the app again from the app drawer, it starts returning ads. When you are ready to go live, just comment out that line, or change “true” to “false”, or just delete this line all together.

AdView

You can find my version of this sample app in my downloads, http://taiic.com/downloads

Join our community on facebook. Just hit the Like in the left sidebar (towards the top of the page), this button

Or directly on our fb page at http://www.facebook.com/pages/tAIICcom/116243311786911

You may also like...

  • I have followed this tutorial as well as every other one that I have found on the net. I still cannot seem to get ads to show in any app that I create. Even trying the sample app that you posted it still does not show any apps. Any suggestions as to what I might be doing wrong? I have imported the code and even tried typing it by hand just to ensure that it was not a simple error. I would think that atleast the sample that you give would show ads when set to test true. Any help would be greatly appreciated.

    • The first time you request an ad, it can take a few hours before you ever see your first one. So you need to use test mode. However, I noticed that test mode with this SDK does not return an ad every time. Just install the app, and keep restarting it. At one point I had to restart it like 20 times (with test on) before I saw an ad.

  • Thank you for that. I was finally able to get the template file to run on the emulator but if I try to run it on my phone hooked up to my computer then it does not return a test ad. Also I am still unable to see ads in my actual app. I have double and triple checked my code to the code that is in the template and we are spot on. One thing that may be causing problems is that I am using nested layouts and views in my main.xml file. I added the linearLayout so that the code was correct. ln my main.xml file I have a scroll view, image view, the new linearLayout and a tablelayout. Is there a special way to nest these so that the ads will display always at the top of my app. I was able to get my header graphic to stay always at the top by moving te outside of the . Thanks again.

    • I don’t think it should impact it. Have you tried using the SDK provided on AdMob?

      • I even tried that. I cannot for the life of me find the breakdown. Would you be willing to let me upload my program source to you so that you could take a look at it and maybe find the problem. I also have been trying to make the sounds in a soundboard be set as a ringtone or notification without any luck. Any help would be greatly appreciated. Thank you.

  • You’ve got a network error. It’s not able to resolve the admob URL for some reason, which results in failure to receive an ad.

  • Hansy50

    You can’t do this with AI apps, can you?

  • Asds

    How can I align the ad banner on the bottom of the screen?

  • Igorsinelnikov

    The AdMob works fine in app but only in the portait mode. When I switch to the landscape it disappears. What is the fix? Thanks.

  • L Oversloot

    Do you know how to implement this with an TabLayout? Can’t get it to work…

    • You would probably want to have it in a separate layout that is not within the TabLayout so that it doesn’t get hidden.

      • L Oversloot

        I want to have it in 1 activity that is under a tab, but I dont know where to put it in the xml, been searching for hours…

        This is the XML as it is without admob…

  • cool!! thanks for you help!

  • but still have small problem here..the admobe appear in addmobe box, says “You must have AdActivity declared in AndroidManifest.xml with configChanges” ..what should I do next??? anyone can help?? really appreciate it..tq 🙂

  • Ducksfan465

    how does this work for app inventor?