In this article, you will learn How To Implement Admob Interstitial Ads Android Studio | How To Add Interstitial Ads.
Interstitial Ads are full-screen ads that cover the interface of an app until closed by the user. They're best used at natural pauses in the flow of an app's execution, such as in between levels of a game or just after completing a task.
Code snippet used in the video :
MainActivity.java
package com.aarappstudios.admobseries;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
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.OnUserEarnedRewardListener;
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;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
public class MainActivity extends AppCompatActivity {
public static final String INTERESTRIAL_AD_UNIT_ID="ca-app-pub-3940256099942544/1033173712";
private InterstitialAd mInterstitialAd=null;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {
}
});
loadInterestrialAds();
mButton=findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mInterstitialAd!=null)
{
mInterstitialAd.show(MainActivity.this);
}
}
});
}
private void loadInterestrialAds() {
AdRequest adRequest=new AdRequest.Builder().build();
InterstitialAd.load(this, INTERESTRIAL_AD_UNIT_ID, adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
super.onAdLoaded(interstitialAd);
mInterstitialAd=interstitialAd;
interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
super.onAdClicked();
}
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
mInterstitialAd=null;
loadInterestrialAds();
}
@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
}
@Override
public void onAdImpression() {
super.onAdImpression();
}
@Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
}
});
}
});
}
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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="Show Interestrial Ads"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
0 Comments