Sunday, October 30, 2011

How to Add AdMob to Your App

AdMob is a service for developers that provides advertisements for your apps.  If you've used Android for any amount of time, I'm sure you know that many apps are monetized through small ads displayed in the app.  So the question is, how can you get your own piece of the mobile ad pie?  Well, I'm about to tell you.
  1. First you'll need to make sure you've got the latest version of the Android SDK.
  2. Next, you need to download the AdMob SDK.  Inside of the zip file, you'll find a .jar file.  Copy that to the libs folder under your project root.
  3. Now go to your project in Eclipse.  Right click your project in the package explorer and choose refresh to make sure the jar is showing up in your libs folder.
  4. Next we need to add the library to the project.  To do this, right click your project and go to properties.  Then go to "Java Build Path" on the left and then choose the "Libraries" tab.  Now click "Add JARs..." and you should see the GoogleAdMobAdsSdk jar file inside of your project.  Select that and click OK until you're back to the editor.
  5. Next we need to add an AdActivity to our AndroidManifest.xml file.  Put this at the bottom of the application section, just before </application>.
    <activity android:name="com.google.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
  6. You also need to set the permissions to access the Internet and the Network State.  Obviously, these are required to download the ads.  This should be pasted at the end of your AndroidManifest.xml right before the </manifest> tag.
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  7. Now it's time to add the ad to your application.  The library we included comes with a new view called an AdView.  Let's add this to our layout through XML.  Open the main layout xml file for your project.  In this example, we'll use a linear layout.  We'll begin by adding the namespace (the xmlns:ads="http://..." part) to the LinearLayout as shown below.
    <LinearLayout android:id="@+id/linearLayout1"
            android:orientation="vertical"
            android:background="#FFFFFF"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
  8. Now go to the bottom of the LinearLayout and add the AdView code shown below.  Remeber to insert your publisher id into the adUnitId field.
    
    
  9. The last tricky part is to use wrap_content for your ad height and then set the main view element to a height of 0px and weight of 1.  This will make the AdView size to the banner ad size and then will allow your main content to take up the remaining space.  In my project, I had a linear layout that was set to fill_parent.  Inside of that I had a fragment with a height of 0px and weight of 1 followed by the AdView with a height of wrap_content.
That's it!  You should now be well on your way to becoming a mobile app mogul.
One thing to look out for for those of you that are running a custom mod on your phone is the etc/hosts file.  Many mods include a list of ad domains that are blocked through the hosts file.  This is great if you don't like seeing ads, but can be a problem when it comes time to test ads in your app.  I used notepad++ and a find/replace with a regular expression like the one below.
Find:
(.*admob.*)
Replace:
# \1
This will comment out all of the admob lines.  I also did it with a search for google instead of admob.

To get your admob ad unit id go to your admob account and click on Sites & Apps and then click the manage button underneath the app you are working with. You'll see the Publisher ID for the app on the details page. This is the Ad Unit ID.


Admob Administration page for an app.

7 comments:

  1. I have a problem with:


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

    It says: error: Error: String types not allowed (at 'configChanges' with value 'keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|
    smallestScreenSize').

    Can You help me?

    ReplyDelete
    Replies
    1. i think u have set your android api level less then 4.

      just remove last two. screenSize|smallestScreenSize.
      it will work.

      Delete
  2. I am using a tabbed activity with webview when i add admob the app simply crashes what to do

    ReplyDelete
  3. Using the phone means different things to different people. Some avoid 800 almost at any cost, or use the phone only for the most necessary of communications. Others are just the opposite, and seem to live with a phone attached to their ear.

    ReplyDelete
  4. Thnx for useful tutorial. Would be nice if you would make one about implementation of GlobalHop sdk cuz I dont really know how to do that

    ReplyDelete
  5. A great piece that sheds much needed light on IoT development services and its impact on business as there are many new details you posted here. Sometimes it is not so easy to build a "IoT development Company" without custom knowledge; here you need proper development skills and experience. However, the details you mention here would be very much helpful for the beginner. Here is yet another top-notch solution provider “X-Byte Enterprise Solutions” who render feasible and credible solutions to global clients.

    Know more here: https://www.xbytesolutions.com/iot-app-development-company.php

    ReplyDelete