Facebook[Meta] Banner Ads implement in Fragment Layout in Android Studio.

In this blog, we must create a banner ad implemented in a fragment layout for a webview app. So, we have to implement it step by step.

android.enableJetifier=true
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
// Facebook[Meta] Ads Dependency
implementation 'com.facebook.android:audience-network-sdk:6.17.0'
<LinearLayout
android:id="@+id/bannerAdLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />

Now, we have to create a package named FacebookAds then under this package, we have to create a Java file named AdsUnitID. After that, we have to create an Ads Unit for banner ads.

public class AdsUnitID {
public static String BANNER = "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID";
public static boolean isAds = true;
// When you want to true and false
public static boolean isBANNER = true;
}

Now, under this package, we have to create a Java file named AdsInitialize. After that, we have to initialize an Ads Unit for banner ads.

public static void showBanner(LinearLayout linearLayout, Context context) {

}
if (AdsUnitID.isAds && AdsUnitID.isBANNER) {
AdView adView = new AdView(context, AdsUnitID.BANNER, AdSize.BANNER_HEIGHT_90);
linearLayout.removeAllViews();
linearLayout.addView(adView);
}

Under this, we have to implement the AdListener method given below.

AdListener adListener = new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {

}

@Override
public void onAdLoaded(Ad ad) {

}

@Override
public void onAdClicked(Ad ad) {

}

@Override
public void onLoggingImpression(Ad ad) {

}
};
adView.loadAd(adView.buildLoadAdConfig().withAdListener(adListener).build());

Now, we have to show a banner ad in a fragment layout.

AdsInitialize.showBanner(view.findViewById(R.id.bannerAdLayout), getContext());
Scroll to Top