Android AdMob Tutorial

This is android admob tutorial.

AdMob is an ad network by Google that allows to monetize mobile apps. In this tutorial I will guide you to integrate admob in android app.

Here you will learn about two types of ads.

Banner Ad: It occupies a small portion of activity.

Interstitial Ad: Occupies full screen. Generally shown while moving from one activity to another.

Android AdMob Tutorial

AdMob Console

Go to https://apps.admob.com and login with your google account.

Now go to Monetize and click on Monetize New App button.

Enter name of app and then create a banner and an interstitial ad unit. You will get id for each ad unit. Just keep it somewhere, we will require it later.

Android AdMob Tutorial 1

Android Project

Create a new android studio project with package name com.admobexample

We have to add dependency for google admob ads. Just add following line of code in build.gradle file under dependency section. Sync the project.

Add internet access permission in AndroidManifest.xml file.

1. Banner Ad

For banner ad we have to use <com.google.android.gms.ads.AdView> widget in layout xml.

Just replace the ad unit id with your banner ad unit id. Make sure the root layout element contains following attribute.

In our activity we have to create an instance of AdRequest and then load it in AdView.

2. Interstitial Ad

For interstitial ad we don’t have to use any widget in layout xml. First make an instance of AdRequest and InterstitialAd. Set ad unit id for interstitial ad and then load the AdRequest inside InterstitialAd. We will add a listener to InterstitialAd instance and show the ad only when it is fully loaded.

Note: When you use a newly created ad unit then it will take some time to start showing ads. Instead of showing live ads you can show test ads. Just read the test ad section at the end of this tutorial.

Add following code in respective files.

activity_main.xml

MainActivity.java

Finally run the app.

The banner ad will be displayed automatically when activity is launched but interstitial ad will be displayed on button click.

Screenshots

Android AdMob Tutorial 2

How to show test ads?

AdMod doesn’t allows you to click on ads yourself. It may be possible that you will accidently click on ads. So in that case your account can be banned. To remain on safer side use test ads while you are developing the app.

You can find following line of code in andorid logcat when you will run the app.

Use AdRequest.Builder.addTestDevice(“BB93E7FC72412E6AF38CD7317F5DA20C”) to get test ads on this device

The string in double quotes is the unique id for the device in which you are running the app. To show test ads just use addTestDevice() method while making AdRequest instance. It can be done in following way.

Replace the string in double quotes with the id that you got from your logcat.

When you are making the app live just remove addTestDevice() method to remove test ads and show live ads.

Comment below if you are facing any difficulty in above android admob tutorial.

2 thoughts on “Android AdMob Tutorial”

Leave a Comment

Your email address will not be published. Required fields are marked *