Admob Interstitial Ads in Android


What are Admob Banner Ads in Android?

Admob is a Google mediation platform. We can use this to display advertisements within our android application. Along with that we can earn money by displaying the ads within our application. There are different types of ads which we can display within our application. In this article we will take a look on How to display Admob Interstitial ads within our android application.

Implementation of Admob Interstitial Ads in Android

We will be creating a simple application in which we will be simply displaying a text view and a button. After clicking that button, users will be able to see the interstitial ads within our application. We will be following a step by step guide to implement a Motion Layout in our Android application using Kotlin.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2 : Adding dependency in build.gradle file to use this library

Navigate to Gradle Scripts>build.gradle file and add the below dependency in dependencies section.

implementation 'com.google.android.gms:play-services-ads:21.5.0'

After adding the above dependency you will get to see the Sync Now option in the top right corner of your IDE. Simply click on it to sync your project and install this dependency within your project.

Step 3 : Adding Admob Application ID in our AndroidManifest.xml file

Navigate to app>AndroidManifest.xml file and add inside the application tag. Add below line.

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

Explanation − In the above code we are specifying the name as application id and passing the value for it as Application ID. Currently we are specifying Admob test app id for the purpose of testing. Later on we can change this by adding the original id provided by Admob.

Step 4 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- on below line creating a text view for heading of application--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/idBtnDisplayAd" android:layout_centerInParent="true" android:layout_margin="10dp" android:text="Admob Interstitial Ads in Android" android:textAlignment="center" android:textColor="@color/black" android:textSize="25sp" android:textStyle="bold" /> <!-- on below line creating a button to display ads--> <Button android:id="@+id/idBtnDisplayAd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:text="Display Ad" android:textAllCaps="false" /> </RelativeLayout>

Explanation − In the above code we are creating our root layout as Relative Layout. Inside this layout we are creating a simple text view to display the heading of our application.

After that we are creating a button which will be used to display interstitial ads when a user clicks this button.

Step 5 : Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>java>your app’s package name.MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterViewFlipper; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; 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 { // on below line creating variable for button. private Button displayAdBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line initialing mobile ads. MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { // on below line displaying a log that admob ads has been initialized. Log.i("Admob", "Admob Initialized."); } }); // on below line initializing variables. displayAdBtn = findViewById(R.id.idBtnDisplayAd); // on below line creating and initializing variable for adRequest AdRequest adRequest = new AdRequest.Builder().build(); // on below line adding click listener for display ads button. displayAdBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // on below line we are loading interstitial ads setting ad unit id to it, ads request and callback for it. InterstitialAd.load(getApplicationContext(), "ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() { @Override public void onAdLoaded(@NonNull InterstitialAd interstitialAd) { // this method is called when ad is loaded in that case we are displaying our ad. interstitialAd.show(MainActivity.this); } @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // this method is called when we get any error Toast.makeText(MainActivity.this, "Fail to load ad..", Toast.LENGTH_SHORT).show(); } }) } }); } }

Explanation − In the above code we are creating a variable for the button which we will be using to display the ad. After that inside the onCreate method we are firstly initializing the mobile ads and displaying a log message inside on initialize method. After that we are initializing that variable with the id which we have given inside our activity_main.xml file. After that we are creating and initializing variables for ad requests. Then we are adding an on click listener for our button to display ads. Inside on click listener we are loading our Interstitial Ads for this we are passing context, ad unit id, ad request and callback to it. Inside the Ad load method we are calling the show method to display our ad. Inside the Ad failed method we are displaying an error toast message.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Conclusion

In the above tutorial we what are Admob Interstitial Ads in android and how we can implement them within our android application and earn money from it.

Updated on: 14-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements