Admob Banner Ads in Fragments using the latest method in Android Studio?

First, we have to create a Banner ADs unit which means we have to use this ads unit anywhere in your Android App project. So, the first step is to insert “Google Admob Dependency” to show Google Ads.

// Admob Ads Dependency
implementation 'com.google.android.gms:play-services-ads:23.3.0'

For better understanding, you must watch a YouTube video.

After Implementing Admob Ads dependency, we have to replace it with the new library.

// Admob Ads Dependency
implementation libs.play.services.ads

After that, we have to implement an Internet Access code and hardware-accelerated system in the “AndroidManifest.xml” file.

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

We have implemented App ID under the application layout.

<!-- 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" />

Below, Hardware Accelerated is implemented under the “application” layout in the AndroidManifest.xml file.

android:hardwareAccelerated="true"

Create a Linear Layout in the custom_webview.xml file that can show banner ads.

 <LinearLayout
android:id="@+id/bannerAdContainer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1" />

Now, Create a new Package named AdmobBannerAds under this package After that we have to create a new Java class named “AdmobAdsUnitID”.

public class AdmobAdsUnit {
public static final boolean AD_STATUS = true;
public static final String ADMOB_BANNER_ID = "ca-app-pub-3940256099942544/6300978111";
}

Now, we have to create a “GoogleAdsManager” class that can load banner ads. Under this class, we have to create a variable that helps to create a show banner ad instantly.

Activity activity;

Now, create a Constructor

public GoogleAdsManager(Activity activity) {
// Implement Constructor
this.activity = activity;
}
public void initAd() {
if (AdmobAdsUnit.AD_STATUS) {
MobileAds.initialize(activity);
}
}

Now, we can implement the banner ad code given below.

public void ShowBannerAd(LinearLayout container) {
if (AdmobAdsUnit.AD_STATUS) {
AdView adView = new AdView(activity);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AdmobAdsUnit.ADMOB_BANNER_ID);
container.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}

Now, we have to implement code to show ads in a fragment layout that why codes are given.

So, first, we have to create a Linear Layout variable that can show ads in a particular Fragment layout.

// Admob Banner Ads Container
LinearLayout bannerAdsContainer;

Now, we have to implement banner ad code which is connected by “GoogleAdsManager“.

// Admob Banner AD
bannerAdsContainer = view.findViewById(R.id.bannerAdContainer);
GoogleAdsManager googleAdsManager = new GoogleAdsManager(getActivity());
googleAdsManager.initAd();
googleAdsManager.ShowBannerAd(bannerAdsContainer);

Now, test this application on your Physical Device.

Scroll to Top