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.
<<<<<<<<<< ===== >>>>>>>>>>
First, we have to implement code under gradle.properties(Project Properties).
android.enableJetifier=true
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
Second, we must implement Facebook[Meta] dependency in the build.gradle(Module :app).
// Facebook[Meta] Ads Dependency
implementation 'com.facebook.android:audience-network-sdk:6.17.0'
Third, we have to create a Linear Layout that can help to show banner ads under Fragment layout.
<LinearLayout
android:id="@+id/bannerAdLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
::::: Java Code Implementation :::::
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.
Now, Open AdsUnitID Java File:
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, Open AdsInitialize Java File:
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, Open the Fragment files one by one:
Now, we have to show a banner ad in a fragment layout.
AdsInitialize.showBanner(view.findViewById(R.id.bannerAdLayout), getContext());
::::: Thank You :::::