ADMOB Ads with Multi WebView App with Fragment Layout in Android Studio | Complete Tutorials
First, we must implement dependency and add some code in the Manifest. file in our project.
NOTE: Requirment “minSDK” – 19 and we have give acess “INTERNET“permission.
<uses-permission android:name="android.permission.INTERNET" />
// Admob Ads Dependency
implementation 'com.google.android.gms:play-services-ads:21.1.0'
<!-- 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"/>
Second, we can create topic-wise details:
- Banner Ads Format.
- Smart Banner Ads Format.
- Medium Rectangle Banner Ads Format.
- Interstitial Ads Format.
- App Open Ads Format.
First, we have to create and implement an XML code in the Activity layout given below.
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="1" />
Second, we have to create and implement a JAVA code in the Activity class given below.
// AdView Variable private AdView mAdView;
// Admob Ads Code Here MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest);

Here, we can XML layout only and all JAVA code are the same even do nat try to change any code in the JAVA file for this Ads format.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adSize="SMART_BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="1" />

Here, we can XML layout only and all JAVA code are the same even do nat try to change any code in the JAVA file for this Ads format.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adSize="MEDIUM_RECTANGLE"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintVertical_bias="1" />

- Implement Interstitial Ads Format.
In this ad format, we have to create JAVA code only. And follow step by step.
Insert the below code under the onCreate method.
// Interstitial Ad
loadInterstitialAd();
Now, we can create a code under AppCompatActivity.
public void loadInterstitialAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
Toast.makeText(HomeActivity.this, "Ad Loaded", Toast.LENGTH_SHORT).show();
interstitialAd.show(HomeActivity.this);
interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
Toast.makeText(HomeActivity.this, "Faild to show Ad", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
Toast.makeText(HomeActivity.this, "Ad Shown Successfully", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
Toast.makeText(HomeActivity.this, "Ad Dismissed / Closed", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdImpression() {
super.onAdImpression();
Toast.makeText(HomeActivity.this, "Ad Impression Count", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClicked() {
super.onAdClicked();
Toast.makeText(HomeActivity.this, "Ad Clicked", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Toast.makeText(HomeActivity.this, "Failed to Load Ad because=" + loadAdError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
Now, we have to create three methods to “onPause”, “onResume” and “onDestroy” under AppCompactActivity.
@Override public void onPause() { if (mAdView != null) { mAdView.pause(); } super.onPause(); } @Override public void onResume() { super.onResume(); if (mAdView != null) { mAdView.resume(); } } @Override public void onDestroy() { if (mAdView != null) { mAdView.destroy(); } super.onDestroy(); }

- Implement App Open Ads Format.
If you really want to create App Open Ads then you must set the fixed dependency given below.
// Admob Ads Dependency
implementation 'com.google.android.gms:play-services-ads:19.6.0'
Now, you can be implemented the above dependency then you have to move coding in java files. So First we have to create a JAVA file named “MyApplication” then you have to insert the below code in your project.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
MobileAds.initialize(this, initializationStatus -> {
});
AppOpenManager appOpenManager = new AppOpenManager(this);
}
}
After that, we have to create one more JAVA file named “AppOpenManager” then you can insert all java code into your project.
public class AppOpenManager implements Application.ActivityLifecycleCallbacks {
private Activity currentActivity;
private static boolean isShowingAd = false;
private static final String LOG_TAG = "AppOpenManager";
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294";
private AppOpenAd appOpenAd = null;
private long loadTime = 0;
private final MyApplication myApplication;
// Constructor
public AppOpenManager(MyApplication myApplication) {
this.myApplication = myApplication;
this.myApplication.registerActivityLifecycleCallbacks(this);
}
// Request an Ads
public void fetchAd() {
if (isAdAvailable()) {
return;
}
AppOpenAd.AppOpenAdLoadCallback loadCallback = new AppOpenAd.AppOpenAdLoadCallback() {
@Override
public void onAppOpenAdLoaded(AppOpenAd ad) {
AppOpenManager.this.appOpenAd = ad;
AppOpenManager.this.loadTime = (new Date()).getTime();
}
@Override
public void onAppOpenAdFailedToLoad(LoadAdError loadAdError) {
}
};
AdRequest request = getAdRequest();
AppOpenAd.load(
myApplication, AD_UNIT_ID, request,
AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
}
// Creates and returns ads request
private AdRequest getAdRequest() {
return new AdRequest.Builder().build();
}
// Utility method to check if ad was loaded more than n hours ago.
private boolean wasLoadTimeLessThanNHoursAgo() {
long dateDifference = (new Date()).getTime() - this.loadTime;
long numMilliSecondsPerHour = 3600000;
return (dateDifference < (numMilliSecondsPerHour * (long) 4));
}
// Check if ad exists and can be shown
public boolean isAdAvailable() {
return appOpenAd != null && wasLoadTimeLessThanNHoursAgo();
}
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
currentActivity = activity;
showAdIfAvailable();
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
currentActivity = activity;
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
currentActivity = null;
}
// Shows the ad if one isn't already showing.
public void showAdIfAvailable() {
if (!isShowingAd && isAdAvailable()) {
Log.d(LOG_TAG, "Will show ad.");
FullScreenContentCallback fullScreenContentCallback =
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
AppOpenManager.this.appOpenAd = null;
isShowingAd = false;
fetchAd();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
}
@Override
public void onAdShowedFullScreenContent() {
isShowingAd = true;
}
};
appOpenAd.show(currentActivity, fullScreenContentCallback);
} else {
Log.d(LOG_TAG, "Can not show ad.");
fetchAd();
}
}
}

————— THANK YOU VERY MUCH —————