HomeTutorialsHow to add interstitial ads in your Android App

How to add interstitial ads in your Android App

When you develop a free app or a free game, the only source of income or revenue are the in-app purchases or in-app ads.

Although in-app purchases require more complex coding, In this tutorial we would be teaching you how you can add interstitial ads in your Android App.

What are Interstitial ads?

According to Google Official docs, 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.

add interstitial ads
add interstitial ads

In short, In any game, if you see a full page add after any game over or on exiting the game, you are viewing an interstitial ad.

Another awesome tutorial:-  Google Login and Registration for Android App

How to add interstitial ads on Android App?

Below we have provided the source code for every important file that requires change or code addition.

AndroidManifest.xml

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-3940256099942544~3347511713"/>

In AndroidManifest.xml file

android:value=“ca-app-pub-3940256099942544~3347511713”

Replace ID with your own.

MainActivity.java

package androidhire.com.interstitialads;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

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


public class MainActivity extends AppCompatActivity {

    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());

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }

        });


    }

    public void mMyButton(View view) {

        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            startActivity(new Intent(this, Main2Activity.class));
        }
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:text="Button"
        android:onClick="mMyButton"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>

Final Words

Finally using these code snippets and by debugging the app on your test Android Phone, you can see if ads are working or not by using the test ads.

Aditya Singh
Aditya Singhhttps://optimizegoal.com
Hi, I’m Aditya Singh. Founder of this blog AndroidHire. I’m Part-Time blogger, and digital marketer. If you want to contribute then you can contact us at contact@androidhire.com

Hot Topics

Related Articles