This week I have been sharing with you a series of articles that are intended to teach you everything you need to know to create your very own Android apps. One day one, I taught you how to download and install the Java Development Kit (JDK), Eclipse, and the Android SDK. On day two, I taught you how to configure Eclipse to work with the Android SDK and how to create your first Android app. Day two was spent teaching you how to create a layout and how to add some code to make your app functional. Day four was spent teaching you how to test your app using the Android emulator. As promised, today’s article will teach you how to monetize your Android app by placing ads in it. So, let’s get right to it.
To begin with, there are plenty of companies out there that provide ads that you can place directly in your Android apps. However, I want to stick with a very simple ad system which also happens to be owned by Google. I’m talking about AdMob. AdMob is a system that allows developers like you to add just a little bit of code to their apps which will display different ads to your end users and will auto-update to provide fresh, new content for enticing users to click on. To get started, you will need to head over to http://www.google.com/ads/admob/ and signup for a free AdMob account. Registration is quick & easy. In fact, if you already have an account with Google, you can login using that account and gain access to the AdMob network.
Once you have an AdMob account and have signed in, click on the “Sites & Apps” tab and select “Add Site / App”. Next, click on the “Android App” icon to select your app type. This will drop down a section labeled “Details”. Go ahead and enter the name of your app, select a category to list it under, add a description for your app, and enter the captcha code. Depending on your app, you will need to either select to use or not to use keyword-target ads. You can also ignore the “Android Package URL” at this point since you haven’t deployed your app to the Play Store yet.
After you have registered your app with AdMob, you will need to get your Publisher ID. This will be used later on. To get your Publisher ID, click on “Sites & Apps” again. Then, at the bottom of that screen, you should see your app listed. Move your mouse over your app’s name until you see the button that says “Manage Settings”. Click on that button.
On the next page, you will see your Publisher ID listed just below the name of your app. Write down this Publisher ID for later. Then, click on the link that reads “Get Publisher Code”. This will take you to a page that provides a button to download the AdMob Android SDK which you will need to do. When you click the button, a file named “googleadmobadssdkandroid.zip” will begin downloading. The file is only about 686KB. So, the download won’t take long. Make sure you download the file to a location you can get to easily.
After you have the AdMob Android SDK downloaded, go to the location you downloaded the ZIP file to and extract it to your filesystem. When the file has finished unzipped, go into the unzipped folder. There you will find a file called “GoogleAdMobAdsSdk-6.3.1.jar”. Keep in mind that 6.3.1 is the version of the SDK at the time of writing this article. So, you may see a file with a different version number depending on when you are reading this article. Copy this file into your project’s “libs” folder.
Next, open your strings.xml file and add a new string entry named “admob_unit_id”. This is where you will need to place the Publisher ID you wrote down earlier. Your entry should look something like this:
Next, open your layout file and switch to the XML view. At the very top of your layout XML, you should have a Layout tag of some kind. In my case, I have a RelativeLayout. You should see a few attributes including “xmlns:android” and “xmlns:tools”. Right below those two lines, you will need to add another namespace for xmlns:ads=”http://schemas.android.com/apk/lib/com.google.ads. Your code should look something like this:
After you have added a reference to the ads namespace, it is time to add the ad itself. To do that, add a new view item somewhere on your activity called “com.google.ads.AdView”. As mentioned in one of the previous articles, I intentionally left an empty spot at the top of my activity big enough to be filled with an advertisement. But, you can decide where you want to place your ads. The view should look something like this:
<com.google.ads.AdView android:id="@+id/adView" android:layout_width="fill_parent" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="@string/admob_unit_id" ads:loadAdsOnCreate="true" android:gravity="left|right|center_horizontal" > </com.google.ads.AdView>
When you swap back over to the Graphical Layout tab, you should see a new box that includes the words “Ads by Google”. By default, the box will have a black background with white text. You can modify the default style by logging into the AdMob site, clicking on Sites & Apps, hovering your mouse over your app name just like before and selecting Manage Settings, then clicking on the “App Settings” tab.
If everything worked correctly, your layout should now include the placeholder for your AdMob ads like this:
Next, open your AndroidManifest.xml file. Somewhere between your “application” tags, you will need to declare the AdActivity from AdMob.
You will also need to add a new “uses-permision” for “android.permission.ACCESS_NETWORK_STATE“. Here is what my entire AndroidManifest.xml file looks like after adding the AdActivity.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.prodigy.mobile.android.udptester" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> <activity android:name="com.prodigy.mobile.android.udptester.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now that you are done with all of the XML changes required to get AdMob working within your app, you have one small code change to make. For that, open up the activity Java file. Inside your “onCreate” method, you will need to add two lines which will call the AdMob SDK JAR you added earlier which will in turn request a new ad. Here are the lines you need to add. Just make sure you use the same ID as the ID for your ads in your layout.
AdView ad = (AdView)findViewById(R.id.adView);
You will also need to make sure you add imports for AdView and AdRequest like this:
That’s it! You now have a way to make money from your Android apps. To verify that your ads are working, go ahead and launch your app using the Android emulator just like you did in yesterday’s article. If everything worked as explained in this article, you should see some ads in your Android app. Here is what the final result of my UDP tester app looks like after adding the AdMob ads.
In the next article of this series, I will show you how to run your new Android app on an actual Android device. Until then, I have shared my entire UDP Tester Eclipse project for you to mess with on your own or to use as a reference for getting your own Android apps working. You can download the full Eclipse project at http://www.prodigyproductionsllc.com/downloads/UDPTester.zip. The only thing you will need to do to make this app work completely is to add your AdMob Publisher ID to the “admob_unit_id” string variable in the strings.xml file. Once you have done that, you will have yourself a fully functioning UDP testing Android app, complete with AdMob adverts.
Create Your Own Android Apps for Fun or Profit
Part 1: Downloading and Installing Java, Eclipse, and the Android SDK
Part 2: Configuring Eclipse and Creating Your First Android App
Part 3: Creating a Layout and Adding Some Code
Part 4: Testing Your App with the Android Emulator
Part 5: Monetizing Your Android App with Google AdMob
Part 6: Running Your Android App on Cellphones and Tablets
Part 7: Publishing Your Android App to the Google Play Store
PayPal will open in a new tab.