In this article I will show you how to Install the Google Mobile Ads SDK into an Android application, we will then integrate the interstitial ads into our android application after the first step above.

In order to integrate the Google Mobile Ads SDK into an Android application we need the following prerequisites:-

  1. Use Android Studio 3.2 or higher
  2. minSdkVersion 16 or higher
  3. compileSdkVersion 28 or higher
  4. You have already created an interstitial ad unit.

After you have done the above steps we can start the integration process.

First, make sure that google() is referenced in the allprojects section of your project-level build.gradle file.

allprojects {
    repositories {
        google()
    }
}

Next, open the app-level build.gradle file for your app, and enter the below statement into the “dependencies” section which will instruct 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.

dependencies {
    implementation 'com.google.android.gms:play-services-ads:19.3.0'
}

Next, 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.

<manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>

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. Do this step in the main activity of your Android application and under the onCreate method.

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
    }
}

The Mobile Ads SDK is now imported and you’re ready to implement an ad. 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.

import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends Activity {

    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    }
}

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. Just make sure you replace the above unit ID with your own ad unit ID before publishing your app.

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

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends Activity {

    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    }
}

To show an interstitial, use the isLoaded() method to verify that it’s done loading, then call show(). The interstitial ad from the previous code example could be shown with the following code.

 if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }

Interstitial ads work best in apps with natural transition points. The conclusion of a task within an app, like sharing an image or completing a game level, creates such a point. Because the user is expecting a break in the action, it’s easy to present an interstitial ad without disrupting their experience. Make sure you consider at which points in your app’s workflow you’ll display interstitial ads and how the user is likely to respond.

That is it! You probably will need to wait for a while before you see the interstitial ad appears on your application!

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:

Leave a Reply

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