tutorial android Google AdMob

tutorial android Google AdMob

AdMob is a multi platform mobile ad network that allows you to monetize your android app. By integrating AdMob you can start earning right away. It is very useful particularly when you are publishing a free app and want to earn some money from it.

Integrating AdMob is such an easy task that it takes not more than 5mins. In this article we’ll build a simple app with two screen to show the different types of ads that AdMob supports.

1. Type of Ads – Banner and Interstitial

AdMob currently support two kinds of ad units. One is Banner ad which occupies a portion of the screen. Other is Interstitial ad which occupies device full screen. Interstitial completely blocks your app UI and places the ad on top it.


2. Creating Ad Units

1. Sign into your AdMob account.

2. Click on Monetize tab.

3. Select or Create the app and choose the platform.

4. Select the ad format either Banner or Interstitial and give the ad unit a name.

5. 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

Create as many ad units required for your app.


Create two ad units, one for banner and other for interstitial as we need to use the ad unit IDs in the next section.

3. Creating New Project

1. Create a new project in Android Studio from File ⇒ New Project. When it prompts you to select the default activity, select Empty Activity and proceed.

2. Open build.gradle and add play services dependency as AdMob requires it.

compile ‘com.google.android.gms:play-services-ads:8.4.0’


3. Add the Ad unit IDs to your strings.xml. Open strings.xml located under res ⇒ values and add the ad units of both Banner and Interstitial.


4. Open AndroidManifest.xml and add the below mentioned permissions and other properties.


> Add google play services version meta-data.

> Add the AdActivity adding configChanges and theme attributes.


3.1 Adding Banner Ad

Banner ads occupies only a portion of the screen. I am adding a banner ad in my main activity aligning to bottom of the screen. In order to add the banner ad, you need to addcom.google.android.gms.ads.AdView element to your xml layout.


5. Open the layout file of your main activity (activity_main.xml) and add the AdView widget. I am also adding a button to launch another in which we’ll try Interstitial ad.


6. Open MainActivity.java and modify the code as shown.

> Create an instance of AdRequest and load the ad into AdView.

> Ad the AdView life cycle methods in onResume(), onPause() and in onDestroy() methods.


Now if you run the app, you should see a banner ad at the bottom of your screen.


3.2 Adding Interstitial Ad (Fullscreen Ad)

Interstitial ads occupies full screen of the app. Adding interstitial ad doesn’t require an AdView element to be added in the xml layout. Rather we load the ad programatically from the activity. Normally these ads will be populated when user is moving between activities or moving to next level when playing a game.

We’ll test this ad by creating a second activity and popup the full screen ad when the second activity is launched.

7. Create an activity named SecondActivity.java by right clicking on package New ⇒ Activity ⇒ Empty Activity.


Now if you run the app, you can see the interstitial ad when the second activity is launched.


3.3 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(“C04B1BFFB0774708339BC273F8A43708”) 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.


3.4 Ad View Listeners

Ad listeners are very useful to perform the next action when ad is closed. Below are the ad listeners can be used to notify your app when ad changes its state.


Download the demo app and start playing until you get enough knowledge about AdMob.


refernsi http://www.androidhive.info/2016/02/android-how-to-integrate-google-admob-in-your-app/[:]

Related Posts
Leave a reply