In this blog, we have to create a Google Admob Interstitial Ad when the back button press ads will be shown and also we have to implement a webview layout in fragment layout for multi-webview app purposes only. If you want to add more apps, you must watch our YouTube video.
For better understanding, you must watch a YouTube video.
In this blog, we have to create a direct Java code file.
::::: Java Code Implementation :::::
First, we must create an Admob Interstitial Ads unit ID in the String.xml file.
<!-- Admob ADs Unit ID -->
<string name="interstitial_Ads">ca-app-pub-3940256099942544/1033173712</string>
Now, Create a new Package named AdmobInterstitialAds under this package After that we have to create a new Java class named “AdsLoadManager”.
All the below code is implemented in the “AdsLoadManager” file.
public static InterstitialAd mInterstitialAd;
public static void loadInterstitialAd(Context context) {
}
Now, we have to initialize Admob Ad SDK. So all codes are implemented under the loadInterstitialAd method.
MobileAds.initialize(context, initializationStatus -> {
});
Now, we have to create an Ad Request method.
AdRequest adRequest = new AdRequest.Builder().build();
Now, we have to initialize to load interstitial Ads.
InterstitialAd.load(context, context.getString(R.string.interstitial_Ads), adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
}
});
}
All the below code is implemented in the “AdsLoadingManager” file.
We have to create an AdsLoadingManager file for loading interstitial ads.
public class AdsLoadingManager extends AppCompatActivity {
}
Now, we have to create an onCreate method for loading interstitial ads.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Now, we have to implement load interstitial ads.
AdsLoadManager.loadInterstitialAd(AdsLoadingManager.this);
Now, we have to replace AppCompactActivity to AdsLoadingManager for loading ads when our App in open.
public class SplashActivity extends AdsLoadingManager
Now, we have implemented interstitial ads code where you to show ads in Fragment Layout when we press the back button.
if (AdsLoadManager.mInterstitialAd != null) {
AdsLoadManager.mInterstitialAd.show(getActivity());
AdsLoadManager.mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
}
});
}