AdMob Integration in Android Application
In this post I will first give a brief introduction of AdMob, then will tell how to integrate AdMob step by step in an Android application. By following these steps you can integrate AdMob in your app within 1 hour.
AdMob Integration Steps:
Step-1: Add Google play service dependencies in app's build.gradle file
compile 'com.google.android.gms:play-services-ads:8.4.0'
Step-2: Create AdMob account if not already created on https://apps.admob.com.
Step-3: Create Ad Unit for the your app. To Create an Ad Unit you need to follow the below steps:
i) Sign into your AdMob account (If you don't have any existing AdMob account then you can create it using your google account on https://apps.admob.com)
ii) Click on Monetize tab.
iii) Select or Create the app and choose Android platform
iv) Select the ad format either Banner or Interstitial and give the ad unit a name.
v) Once the ad unit is created, you can notice the Ad unit ID on the dashboard. An example of ad unit id look like ca-app-pub-0664570763252260/3326342124
Note: If there are two activities in which you need to show Ad then you need to create two Ad Units. So create as many ad units required for your app.
Step-4: Add your Ad Unit in strings.xml file.
Step-5: Open AndroidManifest.xml and add the below mentioned permissions and other properties
i) Add INTERNET & ACCESS_NETWORK_STATE permissions.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />
ii) Add google play services version meta-data inside <application> tag.
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
iii) Add the AdActivity adding configChanges and theme attributes.
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
Step-6: In order to add the banner ad, you need to add com.google.android.gms.ads.AdView element to your xml layout.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_home_footer">
</com.google.android.gms.ads.AdView>
Step-7: In your activity/fragment add the below code
private AdView mAdView;
//In onCreate method
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Step-8: Enabling Test Ads
As per AdMob Policies you are not allowed to click on your own live ads. In order to protect your AdMob account from getting suspended, use test ads during development as you might click the ads accidentally.
When you run the project, if you monitor the LogCat, you can find a similar line Use AdRequest.Builder.addTestDevice(“YOUR_DEVICE_ID”) to get test ads on this device. Copy the device id and add it to AdRequest as shown below. Note that this ID varies from device to device, By doing this, the test ads will be loaded instead of live ads.
In production you need to make sure that you removed addTestDevice() methods in order to render the live ads and start monetization.
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// Check the LogCat to get your test device ID
.addTestDevice("YOUR_DEVICE_ID").build();
Addtional Note: It is a common requirement that AdView should only be visible only when Ads available otherwise not, so to achieve this functionality you should register AdListener in the respective activity/fragment and should show or hide AdView respectivity:
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
mAdView.setVisibility(View.GONE);
}
});
No comments:
Post a Comment