Google AdMob Ads banners use a small portion of the screen to entice users to "click through" to a richer, full-screen experience such as a website or app store page.
To display banners in your Android app, simply incorporate the SDK into your Eclipse project and add a com.google.ads.AdView
to your UI.
The Google AdMob Ads SDK for Android requires Android 1.5 or later. Make sure you have the latest copy of the Android SDK and that you're compiling against at least Android v3.2 (set target
in default.properties
to android-13
).
Incorporating Google AdMob Ads into your app is a three step process:
com.google.ads.AdActivity
in AndroidManifest.xml
.The decompressed SDK consists of a JAR, a javadoc folder and a README.
1. Right click on your app project in Eclipse and chooseProperties.
2. Select Java Build Path and the Libraries tab. Then clickAdd External JARs... to add the Google AdMob Ads JAR.
The AdMob Ads SDK requires that com.google.ads.AdActivity
be declared in your app's AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:label="@string/app_name" android:name="BannerExample"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> </application> </manifest>
Making ad requests requires the networking permissions INTERNET
and ACCESS_NETWORK_STATE
, so these must also be declared in the manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:label="@string/app_name" android:name="BannerExample"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> </application> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> </manifest>
You should now be able to rebuild your project without any errors.
Android apps are composed of View
objects, Java instances the user sees as text areas, buttons and other controls. AdView
is simply another View
subclass displaying small HTML5 ads that respond to user touch.
Like any View
, an AdView
may be created either purely in code or largely in XML.
The five lines of code it takes to add a banner:
com.google.ads.*
AdView
instance The easiest place to do all this is in your app’s Activity
.
import com.google.ads.*; public class BannerExample extends Activity { private AdView adView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create the adView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID); // Lookup your LinearLayout assuming it’s been given // the attribute android:id="@+id/mainLayout" LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); // Add the adView to it layout.addView(adView); // Initiate a generic request to load it with an ad adView.loadAd(new AdRequest()); } @Override public void onDestroy() { adView.destroy(); super.onDestroy(); } }
You can download an example project containing this code here and may alternately create your banner in XML.
When you now run your app you should see a banner at the top of the screen:
Note: The very first time AdMob sees your publisher ID it may take up to two minutes to receive an ad. This initial two minute lag will recur every time the ID goes unused for 24 hours.
Warning: All new Android apps created after October 14, 2011 will require an AdMob SDK that was released on or after March 15, 2011. This corresponds to version 4.0.2+ for Android. If you downloaded the library from our official download site, then you're already set. Otherwise you may have an old version of the AdMob SDK that was released prior to March 15, 2011, and your new app will not receive any ad impressions until you update your SDK.