Android Studio Examples

100+ tutorials, source code, and examples to help you develop creative and technical skills.

How to integrate Admob Interstitial Ads in Android Application

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.learnoset.googleinterstitialads">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GoogleInterstitialAds">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--- Replace value with your orignal App Id-->
        <!---But while testing your application you must use test app id-->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713" />
    </application>

</manifest>

gradle.build (app)

dependencies {
    
    // implement Google Admob Dependency
    implementation 'com.google.android.gms:play-services-ads:20.5.0'
}

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">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/showAdBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Ad"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.learnoset.googleinterstitialads;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends AppCompatActivity {

    private InterstitialAd interstitialAd2;

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

        final Button showAdBtn = findViewById(R.id.showAdBtn);

        // add below code in your launcher Activity. If you have a SplashScreen activity then add this line in that Activity
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        // Requesting for Ad
        AdRequest adRequest = new AdRequest.Builder().build();

        // loading requested ad
        InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        Toast.makeText(MainActivity.this, "Ad Loaded", Toast.LENGTH_SHORT).show();

                        // assign object
                        interstitialAd2 = interstitialAd;

                        // After ad has loaded then show this ad over the activity
                        // If you want to show ad on Button click then
                        interstitialAd.show(MainActivity.this);

                        // registering ad callback
                        interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                            @Override
                            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                                super.onAdFailedToShowFullScreenContent(adError);
                                Toast.makeText(MainActivity.this, "Failed to show Ad", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdShowedFullScreenContent() {
                                super.onAdShowedFullScreenContent();
                                Toast.makeText(MainActivity.this, "Ad Shown Successfully", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdDismissedFullScreenContent() {
                                super.onAdDismissedFullScreenContent();
                                Toast.makeText(MainActivity.this, "Ad Dismissed / Closed", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdImpression() {
                                super.onAdImpression();
                                Toast.makeText(MainActivity.this, "Ad Impression Count", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAdClicked() {
                                super.onAdClicked();
                                interstitialAd2 = null;
                                Toast.makeText(MainActivity.this, "Ad Clicked", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        interstitialAd2 = null;
                        Toast.makeText(MainActivity.this, "Failed to Load Ad because = " + loadAdError.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

        showAdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // check if value is not null
                if (interstitialAd2 != null) {
                    // use show method in onClick
                    interstitialAd2.show(MainActivity.this);
                }

            }
        });
    }
}

Projects with Source Code + Video Tutorials

You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Simple-Music-Player-App-for-Android-(Offline-Version)
Simple Music Player App for Android (Offline Version)
Login-and-Register-Activity-Material-UI-design-in-Android-Studio
Login and Register Activity Material UI design in Android Studio
Login-and-Register-screen-with-OTP-Verification-Material-UI-design
Login and Register screen with OTP Verification Material UI design

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.