How to add admob interstitial ad in your android app using android studio ?

Add admob interstitial ad in your android app

Add admob interstitial ad in your android app

This tutorial we guide you to integrate the Google Mobile Ads SDK into a new app. And show you how to display an interstitial ad in your app.

Step 1

Login to your https://www.google.com/admob/ account the  click Monetize a new app.

Step 2

Click add your app manually if it is not deployed in google playstore

Enter app name and platform

monitize-new-app

Click Add app

interstitial

Generate & Save App ID & Ad unit ID for interstitial ad

Skip firebase if you don’t need it now.

Click Done

Step 3

Open AndroidManifest.xml and add the below mentioned permissions and other properties.

  • Add INTERNET & ACCESS_NETWORK_STATE permissions.
  •  Add google play services version meta-data.
  • Add the AdActivity adding configChanges and theme attributes.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.appname">
 
    <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:supportsRtl="true"
        android:theme="@style/AppTheme">
 
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
 
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <!--Include the AdActivity configChanges and theme. -->
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>
 
</manifest>

Step 4

Now launch Android studio & open your app.

Open your app’s  build.gradle(Module:app)

Add firebase dependency   // Google integrate admob with firebase

dependencies 
 {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 
 compile 'com.android.support:appcompat-v7:24.2.1'
 compile 'com.android.support:design:24.2.1'
 compile 'com.google.firebase:firebase-ads:9.0.2' // Firebase dependency (Always check for the latest version)
 }
apply plugin: 'com.google.gms.google-services'

P.S Always check for the latest firebase dependency (if you have trouble with new one use 9.0.2)

Now sync your project

Step 5

Open your activity java code

package com.sample.appname;

import android.support.v7.app.AppCompatActivity;

import com.sample.appname.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;        //import these 4 libraries        
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.InterstitialAd;

public class Mainactivity extends AppCompatActivity 
{

InterstitialAd mInterstitialAd;

 @Override
    protected void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        MobileAds.initialize(getApplicationContext(), "ca-app-pub- 65899583587752154~78858584206"); // Add your App ID here (Don't confuse with adunit ID)     

       

          mInterstitialAd = new InterstitialAd(this);
       // mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_id)); // use this if you want to load id from string resource

        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");  // Use your ID here in production version


        requestNewInterstitial();


    }

     private void requestNewInterstitial()
      {

       AdRequest adRequest = new AdRequest.Builder()
               .addTestDevice("PFSW5FDD5PRTS9TTO")  // Add your device ID  from LOGCAT here // Remove this line in production version
               .build();

       mInterstitialAd.loadAd(adRequest);
      }



    private void showInterstitial()    // Call this function on your add show event
     { 
        if (mInterstitialAd.isLoaded())  
        {
            mInterstitialAd.show();
        }

         requestNewInterstitial(); // request for new interstitial
    }


}

Don’t forget to call below function (showInterstitial()) on the event which you want ad to display

 private void showInterstitial()    // Call this function on your add show event
     { 
        if (mInterstitialAd.isLoaded())  
        {
            mInterstitialAd.show();
        }

         requestNewInterstitial(); // request for new interstitial
    }

 

Example:


public void settings(View v) // Button click event
{
showInterstitial()

}

adsampfle