1

Example code provided in https://firebase.google.com/docs/admob/android/banner works fine for me using a given advertiser ID and ad size in xml file. But it is very important for me to be able to set advertiser ID and banner size based on certain parameters.

I considered these two but they did not work for me ->

Programatically set the AdMob id String and

How to specify adUnitId programmatically for AdMob?

When I try to implement second one I get errors when I try to link java file to xml file. How they should be linked?

Community
  • 1
  • 1
Ula
  • 2,628
  • 2
  • 24
  • 33

1 Answers1

2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:background="@null"
        android:layout_height="wrap_content" />
</LinearLayout>

AdView mAdView = new AdView(context);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId(AD_UNIT_ID);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.adView);
frameLayout.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
K. Gandhi
  • 176
  • 6