Once your Android application is developed and ready to publish and you want to make some money from the application by integrating google Ads init, you need to learn how to do that. Then this article will help you. In this article, you will learn how to integrate AdMob in an Android application. By the end of this article, you will know how to integrate 3 different ads in android application, Banners ads, Interstitial Ads, and Rewarded Video Ads.

Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue. Once you’ve integrated the SDK, you can choose an ad format (such as native or rewarded video) and follow the steps to implement it.

Setup AdMob for your android application

Prerequisites

In the below image, we have added our android app.

Android Application is added in the AdMob Account.

Import the Mobile Ads SDK

Apps can import the Google Mobile Ads SDK with a Gradle dependency that points to Google’s Maven repository. First, make sure that google() is referenced in the allprojects section of your project-level build.gradle file.

Example project-level build.gradle

 

Next, open the app-level build.gradle file for your app, and look for a “dependencies” section.

Example app-level build.gradle

Add the AdMob dependency above, which instructs Gradle to pull in the latest version of the Mobile Ads SDK and additional related dependencies. Once that’s done, save the file and perform a Gradle sync.

Update your AndroidManifest.xml

Add your AdMob App ID to your app’s AndroidManifest.xml file by adding a <meta-data> tag with name com.google.android.gms.ads.APPLICATION_ID, as shown below.

You can find your App ID in the AdMob UI. For android:value insert your own AdMob App ID in quotes, as shown below.

Above is an example of adding the <meta-data> tag in your AndroidManifest.xml, which should be added in the application tag.

Initialize Mobile Ads SDK

Before loading ads, have your app initialize the Mobile Ads SDK by calling MobileAds.initialize() which initializes the SDK and calls back a completion listener once initialization is complete (or after a 30-second timeout). This needs to be done only once, ideally at the app launch.

Here’s an example of how to call the initialize() method in an Activity:

If you’re using mediation, wait until the completion handler is called before loading ads, as this will ensure that all mediation adapters are initialized.

Implement Banner Ads

Banner ads occupy a spot within an app’s layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time.

Add AdView to the layout

The first step toward displaying a banner is to place AdView in the layout for the Activity or Fragment in which you’d like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here’s an example that shows an activity’s AdViewin the activity_main.xml:

Note the following required attributes:

  • ads:adSize: Set this to the ad size you’d like to use. If you don’t want to use the standard size defined by the constant, you can set a custom size instead. See the banner size section below for details.
  • ads:adUnitId: Set this to the unique identifier given to the ad unit in your app where ads are to be displayed. If you show banner ads in different activities, each would require an ad unit.

You can alternatively create AdView programmatically:

Always test Banner Ads with test ads

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

The easiest way to load test ads is to use our dedicated test ad unit ID for Android banners:

It’s been specially configured to return test ads for every request, and you’re free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK’s test ads work, see Test Ads.

Load a Banner ad

Once the AdView is in place, the next step is to load an ad. That’s done with the loadAd() method in the AdView class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.

Here’s an example that shows how to load an ad in the onCreate() method of an Activity:

That’s it! Your app is now ready to display banner ads.

Ad events of Banner Ad

To further customize the behavior of your ad, you can hook onto a number of events in the ad’s lifecycle: loading, opening, closing, and so on. You can listen for these events through the AdListener class.

To use an AdListener with AdView, simply call the setAdListener() method:

Each of the overridable methods in AdListener corresponds to an event in the lifecycle of an ad

Out of Banner Ad

.

Implement Interstitial Ads

Interstitial ads are full-screen ads that cover the interface of their host app. They’re typically displayed at natural transition points in the flow of an app, such as between 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.

Create an interstitial ad object

Interstitial ads are requested and shown by InterstitialAd objects. The first step is instantiating InterstitialAd and setting its ad unit ID. This is done in the onCreate() method of an Activity:

A single InterstitialAd object can be used to request and display multiple interstitial ads over the course of an activity’s lifespan, so you only need to construct it once.

Always test Interstitial Ad with test ads

The easiest way to load test ads is to use our dedicated test ad unit ID for Android interstitials:

ca-app-pub-3940256099942544/1033173712

Load an Interstitial ad

To load an interstitial ad, call the InterstitialAd object’s loadAd() method. This method accepts an AdRequest object as its single parameter:

Show the Interstitial ad

Interstitial ads should be displayed during natural pauses in the flow of an app. To show an interstitial, use the isLoaded() method to verify that it’s done loading, then call show(). The interstitial ad example could be shown in a button’s OnClickListener like this:

Output of Intertitial Ad

Intertitial Ad will launched once you click on the start button.

Ad events of Interstitial Ad

To further customize the behavior of your ad, you can hook onto a number of events in the ad’s lifecycle: loading, opening, closing, and so on. You can listen for these events through the AdListener class.

To use an AdListener with an InterstitialAd object, simply call the setAdListener() method:

 

Each of the overridable methods in AdListener corresponds to an event in the lifecycle of an ad.

Using an AdListener to reload the Interstitial Ad

The AdListener class’s onAdClosed() method is a handy place to load a new interstitial after displaying the previous one:

That’s it! This is all about displaying interstitial ads.

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.

Initialize rewarded video ads

A RewardedVideoAd object can be retrieved using MobileAds.getRewardedVideoAdInstance().

Request rewarded video ad

Adhering to the singleton design of RewardedVideoAd, requests to load an ad should be made to the shared instance.

It is highly recommended that you call loadAd() as early as possible (for example, in the onCreate() method of your Activity) to allow videos to be preloaded.

Always test Rewarded Video with test ads

The easiest way to load test ads is to use our dedicated test ad unit ID for Android rewarded video:

ca-app-pub-3940256099942544/5224354917

Set up event notifications for Rewarded Video Ad

The SDK provides the RewardedVideoAdListener interface, which has methods corresponding to the events in a rewarded video ad’s lifecycle. Have your app define a class that implements this interface and pass it to setRewardedVideoAdListener prior to loading an ad.

The code example in Initialize rewarded video ads already shows how to declare that your class implements RewardedVideoAdListener and set the listener on the RewardedVideoAd object. Here is a sample implementation of the listener methods:

Display a rewarded video ad

We recommend that you ensure that a rewarded video ad has finished loading before attempting to display it. The isLoaded() method indicates if the rewarded video ad request has been successfully fulfilled.

Output of RewardedVideo Ad

Using a RewardedVideoAdListener to reload a Rewarded Video ad

The RewardedVideoAdListener class’s onRewardedVideoAdClosed() method is a handy place to load a new rewarded video ad after displaying the previous one:

Forward lifecycle events for Rewarded Video Ad

To forward the parent Activity’s lifecycle events to the RewardedVideoAd object, call the resume()pause(), and destroy() methods in the parent Activity’s onResume()onPause(), and onDestroy() methods respectively.

Here is an example of the Activity lifecycle event forwarding:

That’s it! This is all about displaying Rewarded Video Ads.

Useful Link for AdMob: Mobile Ads SDK (Android)

Summary

In this article, you have learned how to integrate AdMob in the android application. Now, you know how to integrate 3 different ads in android application, Banners ads, Interstitial Ads, and Rewarded Video Ads.