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.

Recent Articles

8 Best Offline Football Games for Android 2020

Are you looking for the best offline football games? Football is one of the world's most popular games. Even it is one...

Samsung Galaxy S20+ leaked, image surface online

Just a few days beofre Samsung is going to showcase the new S20 series, Galaxy S20+ leaked images online somehow showing the...

Samsung Galaxy Note 10 lite: 2 Year old processor smartphone, Best Mid ranger?

Earlier this week Samsung unveiled Samsung Note 10 lite and Samsung Galaxy S10 lite. It was very surprising as Galaxy lineup has...

Easy Trick: How to Merge Finder windows in Mac

If you have used old MAC OS then you might remember you can't merge the windows and it was very difficult to work...

[Updated] Temporarily Free Games and Apps on Google Play Store

Every week there are free games and apps on the Google Play store. Here is the list of free games and apps on the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay on Top - Get the daily news in your inbox