How to Integrate Google AdMob into Android App

As an Android developer, you would have thought about how can you easily make money out of your Android App. Google AdMob is one of the best ways to monetize your Android application. In this tutorial, I will explain in detail about integrating Google AdMob to Android application.

After going through this tutorial, you will come to know:

  • How to display Banner Ads on Android.
  • How to display Interstitial / Full-Screen Ads on Android.
  • How to display Rewarded Video Ads on Android.

Types of AdMob Ads:

Currently, there are 4 types of AdMob ads

Banner Ads are rectangular image or text ads that occupy a spot within an app’s layout. Banner ads are the most common ads seen on Android apps. They are usually displayed on the bottom of the screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time.

Interstitial / Full-Screen Ads

Interstitial Ads are full-screen ads that cover the interface of their host app. The word Interstitial means “space between structures or objects”. Hence, Interstitial Ads are Ads which are displayed between two activities or during the pause between levels in a game. When an app shows an interstitial ad, the user has the choice to either tap on the ad and continue to its destination or close it and return to the app.

Native Ads

Native Ads can be used to customize fonts, colors, and other details of the Ads so that user experience is improved. AdMob offers two different ways to implement native ads:

Native Ads Express

The native express ad format is being discontinued and developers are advised to use other Ad formats.

Native Ads Advanced

Native Ads Advanced is currently in a limited release. We will update this article once it is released in full fledge.

Rewarded Video Ads

Rewarded Video Ads are full-screen video ads that users have the option of watching in full in exchange for in-app rewards. These ads are useful in games when the user moves from one level to another and some game points/coins can be rewarded if the user views the video ad.

Creating a new Project

Create a new Project in Android Studio with the name AdMob Integration. Android Studio provides easy steps to integrate AdMob into your app:

1. From the menu, open Tools => Firebase

2. Now an assist window will appear as shown below. Expand Admb and click on Add Banner ad to your app.

Adding-AdMob-to-App---Step-1

3. Now click on Connect to Firebase.

Adding-AdMob-to-App---Step-2

If you are setting up firebase first time in your application then you need to connect your app to Firebase. For this, click on “Connect to Firebase”. Now your browser will open and you will be asked to log in to your Google Account.

4. Once you have successfully logged in, you will be prompted to create a new Firebase Project as shown below:

Adding-AdMob-to-App---Connecting-to-Firebase

5. Provide the app name as AdMobIntegration, choose your country from the drop-down, and click on Connect to FIrebase.

6. Now click on Add Admob to your app. You will be prompted with a window which shows the dependencies that will be added as part of AdMob as shown below:

Admob-integration---Adding-dependencies

Displaying Banner Ads

1. Open strings.xml from res folder and add the following strings:

Here admob_app_id and banner_ad_unit_id are used to display test Ads. I will explain later in this article on how to display real ads.

2. Update main_activty.xml with the following code. com.google.android.gms.ads.AdView is the View used to display banner ads. Inside the view, I have specified the adUnitId.

3. Open MainActivity.java and update it with the following. In the initializeBannerAd method, I have added an AdListener, which has different ad lifecycle methods, which can be used to further customize the behavior of your ad.

4. Now run the application on your Android device and you should be able to see the banner Ad as shown below:Screenshot-displaying-Banner-Ads

Displaying Interstitial/Full-Screen Ads

I will demonstrate displaying interstitial ad on click of a button.

1. Add the test ad unit id to strings.xml:

2. Add a button to activity_main.xml

3. In MainActivity, I am displaying Interstitial Ad, whenever the button is clicked. Similar to Banner Ads here also, I have set Ad listeners. The method onAdClosed is used to call loadInterstitialAd to load the ad so that it can be displayed when the button is clicked next time.

4. Now run the application to see the full-screen ad:Screenshot-displaying-Interstitial-Ads

Displaying Rewarded Video Ads

Rewarded Video Ads are implemented in similar to Interstitial ads.

1. Add test ad unit id to strings.xml

2. Add a button to activity_main.xml

3. Update MainActivty with the following code. For Reward Video Ads, we need to RewardedVideoAdListener in order to handle lifecycle events instead of AdListener. The onRewarded callback can be used to reward the user.

4. Now run the application. Demonstration of Reward Video Ads can be seen in the video demo at the end of the article.

Obtaining App Id from AdMob and Running Live, Production Ads

Warning: When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your AdMob account.

Creating AdMob App Id

For displaying the live ads, you need to register for AdMob account and get your App Ids.

1. Go to admob.com and register yourself for an account.

2. Once you have finished registration, go to AdMob dashboard and open Apps from the sidebarAdmob---Adding-first-App

3. Now click on Add Your First App button. Now from the below options, select No since the app is still in development phase.Admob---Adding-first-App---Step-2

4. Now provide the app name and click on Add.

Admob---Adding-first-App---Step-3

5. Now you will see the AdMob App Id displayed. Copy it and paste it into the admob_app_id field in string.xml.

Creating AdMob Ad Units

In order to get Ad Unit Id, you need to create an Ad Unit.

1. Open the newly created App from the list apps in the AdMob dashboard and click on Add Ad Unit.

2. From the options, choose the type of Ad Unit you wish to create:

Admob---Creating-Rewarded-Ad-Unit---Step-1

3. Now provide the ad unit name and click on Create Ad Unit.

Admob---Creating-Ad-Unit---Step-3

On successful addition of Ad unit, you will be displayed with ad unit Id:

Admob---Creating-Ad-Unit---Step-3

Adding Payment Details

On home page AdMob dashboard, you might see an error message as shown below:

Admob-Add-Payment-Info

Click on Fix It it and add payment details. Otherwise, your ads will not be displayed.

Linking Firebase to Admob [Optional]

You may link your Admob App to Firebase to improve App monetization and user experience. To do so,

1. Open on your app in the AdMob dashboard and click on App Settings from the sidebar and click on Link to Firebase.

Admob-Link-to-firebase

2. Provide the package name and continueAdmob-Link-to-firebase---Package-name

3. From the existing apps, choose your app and click on Continue:Admob-Link-to-firebase---App-Details

Demonstration

In the below video you can see the demonstration of the whole tutorial. I have combined all ad types in 1 app:

Source code and APK files

You can download the Source code and APK files from the below links.

Download Source CodeDownload APK

If you have any doubts, then we can discuss them in the comments section. If you have liked this tutorial, please do follow us on Twitter and Facebook.

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Leave a Reply

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