Home Tutorials How to add interstitial ads in your Android App

How to add interstitial ads in your Android App

How to add interstitial ads

Find out the special power of including Google’s Interstitial Ads in your Android apps. This easy-to-follow guide will show you the right way to add these big, full-screen ads without messing up the fun your users are having. If you put these ads in just the right spots, they can get more clicks and interactions, which means more money for you.

What Are Interstitial Ads?

Interstitial Ads are full-screen ads that cover the entire interface of an Android app. Visually engaging and intriguing, they are typically displayed during natural transition points like the conclusion of a game level or while waiting for a new page load. These ads can effectively capture user attention and, if placed strategically, can highly increase your ad revenue.

Preliminary Steps: Integrating Google Mobile Ads SDK

Before embarking on the journey to add Interstitial Ads in your Android app, you need to integrate the Google Mobile Ads Software Development Kit (SDK) into your application.

Updating your Android Studio

Ensure you have the latest version of Android Studio. It’s essential to have updated tools for seamless integration.

Adding Google Play Services to your Project

Include the Google Play services package into your Project’s build.gradle file. This package is necessary because the Google Mobile Ads SDK is a part of Google Play services.

Adding Interstitial Ads in your Android App

With the prerequisites done, now here’s how you can add Interstitial Ads to your Android App.

Importing the Google Mobile Ads SDK

First, import the Google Mobile Ads SDK in the app-level build.gradle file. Once the Gradle project sync is successful, the SDK is ready to be used.

Adding Interstitial Ads elements

The next step involves creating the Interstitial Ad object, loading it with an ad, and then showing it when needed.

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

How To Add Interstitial Ads In Your Android App

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

First, you need to update your AndroidManifest.xml file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <!-- The following lines are required for interstitial ads -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"> 
        <!-- Make sure you replaced 'com.example.myapp' with your package name -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="YOUR_ADMOB_APP_ID"/>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Remember to replace "YOUR_ADMOB_APP_ID" with your actual AdMob App ID.

MainActivity.java file to create and load the ad. Here’s a basic example:

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,
        new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        AdRequest adRequest = new AdRequest.Builder().build();

        InterstitialAd.load(this,"YOUR_ADMOB_UNIT_ID", adRequest,
            new InterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                mInterstitialAd = interstitialAd;
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                mInterstitialAd = null;
            }
        });
    }
     public void mMyButton(View view) {

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

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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" />

</androidx.constraintlayout.widget.ConstraintLayout>

Please adjust the method name in android:onClick to match your method in MainActivity.java. If you don’t have a method called mMyButton in your MainActivity file, you will get a runtime error when clicking the button. So, ensure that a suitable corresponding method exists!

Final Words

Adding Interstitial Ads to your Android app might be a good idea if you do it right. These ads are like full-page ads that pop up at certain times in your app. You need to know where to put these ads and when to show them to make your app better and to earn more money. This guide will help you learn how to add Interstitial Ads to your app in the best way.

Remember, it’s important to find a good balance. You want your ads to work well, but you don’t want to annoy your app users. Each app is unique, so you’ll need to take some time to think about the best way to use Interstitial Ads in your own app.

Exit mobile version