3

I am an beginner and In my app i need to change admob banner unit id from edittext. I really don't know how can i do this. Currently this is the AdView xml code :

<com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="75dp"/> 

and this is how i am setting the adview code :

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {                
                    id = editText.getEditableText().toString();
                    adView.setAdSize(AdSize.BANNER);
                    adView.setAdUnitId(id);
                    AdRequest adRequest = new AdRequest.Builder().build();
                    adView.loadAd(adRequest);
            }
        }); 

how can i retrive and set the id from edittext and show the banner ad ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
wiils
  • 143
  • 2
  • 12

1 Answers1

0
JAVA 
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

KOTLIN
val adView = AdView(this)
adView.adSize = AdSize.BANNER
adView.adUnitId = "ca-app-pub-3940256099942544/6300978111"
  • 1
    this will create a new adview ? then what about the position of adview where it will be ? – wiils Jul 06 '18 at 07:32
  • 1
    @wiils this answer will help you https://stackoverflow.com/questions/24766257/how-to-specify-adunitid-programmatically-for-admob – Dyno Cris Aug 16 '21 at 19:07