Android Projects

Android Development Books

Tuesday

How to integrate Admob into your app in android


AdMob is one of the world's largest mobile advertising networks, offering solutions for discovery, branding and monetization on mobile phones.
The AdMob Android SDK contains the code necessary to install AdMob ads in your application.
Step 1
   In your project's root directory create a subdirectory libs . This will already be done for you if you used Android's activitycreator tool. Copy the AdMob JAR (admob-sdk-android.jar) file into that libs directory.
For Eclipse projects:

  1. Right-click on your project from the Package Explorer tab and select Properties
  2. Select Java Build Path from left panel
  3. Select Libraries tab from the main window
  4. Click on Add JARs...
  5. Select the JAR copied to the libs directory
  6. Click OK to add the SDK to your Android project                                                                     
Step 2
 Add your publisher ID to your AndroidManifest.xml . Just before the closing </application> tag add a line to set your publisher ID. If you publisher ID were 149afxxxx, the line would look like this:
<meta-data android:value="a149afxxxx" android:name="ADMOB_PUBLISHER_ID"/> </application>
To find your publisher ID, log into your AdMob account, select the Sites & Apps tab, and click on the Manage Settings link for your site. On this page, you can find your publisher ID as shows figure 1.  

figure 1. Showing where you can find your Publisher ID

Step 3

Add the INTERNET permission to your AndroidManifest.xml just before the closing </manifest> tag:
<uses-permission android:name="android.permission.INTERNET" /> </manifest>
Optionally, you can add the ACCESS_COARSE_LOCATION and/or ACCESS_FINE_LOCATION permissions to allow AdMob the ability to show geo- targeted ads.
Your final AndroidManifest.xml may look something like figure 2.




figure 2. This is how it would look after pasting some code

Step 4

Paste the following into your attrs.xml file:
<declare-styleable name="com.admob.android.ads.AdView"> 
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" /> 
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" /> 
</declare-styleable>
If your project does not already have an attrs.xml file, then create one in the /res/values/ directory of your project, and paste the following:
<?xml version="1.0" encoding="utf-8"?> <resources>
<declare-styleable name="com.admob.android.ads.AdView"> 
<attr name="backgroundColor" format="color" /> 
<attr name="primaryTextColor" format="color" /> 
<attr name="secondaryTextColor" format="color" /> 
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" /> 
</declare-styleable>
</resources>


Step 5


Create a reference to the attrs.xml file in your layout element by adding xmlns line that includes your package name specified in AndroidManifest.xml . For example, if your package name werecom.example.sampleapp you would include this line:
xmlns:myapp="http://schemas.android.com/apk/res/com.example.sampleapp"
So for a simple screen with only one ad, your layout element would look like this:
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.example.SampleApp"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.admob.android.ads.AdView 
android:id="@+id/ad"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
myapp:backgroundColor="#000000" 
myapp:primaryTextColor="#FFFFFF" 
myapp:secondaryTextColor="#CCCCCC"
</LinearLayout>
/>


When integrating AdMob ads into your application it is recommended to use test mode. In test mode test, ads are always returned. Test mode is enabled on a per-device basis. To enable test mode for a device, first request an ad, then look in LogCat for a line like the following:
To get test ads on the emulator use AdManager.setTestDevices...
Once you have the device ID you can enable test mode by calling in your main activity AdManager.setTestDevices:
AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR, "E83D20734F72FB3108F104ABC0FFC738", //Phone ID 
} );
}
Once you have successfully requested test ads, try clicking on each type of test ad to make sure it works properly from your application. The type of test ad returned is changed withAdManager.setTestAction. You can see the result in figure 3.


figure 3. The result of this snippet

Would you want see more this article click here.

See Also




































      
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0