NOTE: You must use Android Studio Version 4.2 Beta 2 or above.

Before we create the latest Google Admob Interstitial Ads, first we have to set up a new project for this. So first we create a project in Android Studio Software. All steps are given below with an image for your better understanding purpose.

STEP: 01

You can click on “Create New Project” then jump next page.

STEP: 02

Now, you must choose “Empty Activity” and then click the Next button and then you can jump next screen/page.

STEP: 03

Now, you can write “Application Name“, “Package Name“, “Save Location“, “Language – JAVA“, “Minimum SDKAPI 21“. After customization of all things then you can click the Finish button. And then you can wait a few minutes while your project is Sync successfully.

STEP: 04

Double click on “manifests” folder.

STEP: 05

Add some use permission for CONNECTIVITY. means you must create an “Internet permission” method then your ads will show in your mobile application.

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

STEP: 06

Add Google Admob Ads meta-data to show your ads in the application. You must insert meta-data code under the application section.

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

STEP: 07

Now, we can add Admob ads dependency that’s why we can insert the latest Admob dependency. Now double click on Gradle Scripts and again double click on build.gradle(Module: CLICK2CODE.app).

STEP: 08

Here CLICK2CODE is my project name you can click as per your project name and insert the under dependency section given below.

// GOOGLE ADMOB ADS DEPENDENCY
implementation 'com.google.android.gms:play-services-ads:19.6.0'

STEP: 09

Now, all setups will be inserted successfully then you can click the “Sync Now” button which shows the right side top corner of your project screen.

IMPLEMENT ON BACK PRESS BUTTON METHOD SHOW INTERSTITIAL ADS

First, we have to go activity_main.xml layout, and here we can create a “BUTTON” to enter the next activity then we can go back using the mobile back press system. You can also use the below code for time-saving purposes.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">

<Button
android:onClick="nextActivity"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Next Activity"
android:textColor="@color/white"
android:textStyle="bold" />

</RelativeLayout>

Now, we can create a new activity named MainActivity2.java ( you can put your choice name ) then you can open MainActivity.java and create a method to open new activity when we click/press the button.

public void nextActivity(View view) {
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}

After finishing the above process then you can open MainActivty2.java that we created. In this MainActivity2.java we can create and load interstitial ads to show when we press the back button on our mobile.

So, first of all we create a variable for interstitial ads.

private InterstitialAd mInterstitialAd;

After that, we can create an interstitial ads code to show and load ads when we press the back button.

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() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});

Now, you must create an onBackPressed method to show interstitial ads whenever we press the back button. This method applies the below code.

IF YOU HAVE ANY DOUGHT THEN YOU MUST WATCH OUR YOUTUBE VIDEO CLICK HERE.

@Override
public void onBackPressed() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
super.onBackPressed();
}

After inserted all code, we can finish this project now you can run the project on your real mobile device and test your ads.

*** THANK YOU ***


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *