Close ad by clicking close button

Asked

Viewed 182 times

0

I would like to know how to close the layout adview if the user clicks on the ad "X".

My ad code is the below and use in the main layout activity_main:

    MobileAds.initialize(getApplicationContext(), getString(R.string.ID_APP_ADMOB));
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

I use that code.

My xml is the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xxx.android.MainActivity"
android:padding="0dp"
android:orientation="vertical"
android:weightSum="1"
android:background="#2c3e50">

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"
        android:visibility="invisible"/>

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="invisible"
        android:indeterminate="false" />

</FrameLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/ID_ANUNCIO_BANNER">
</com.google.android.gms.ads.AdView>

I tried to implement this question: Disable admob ads after a click?(In English) but not successful!

Can someone help me with that question?

1 answer

1


Man, by the X of the ad I believe you can’t control

What you can do is set an ID in the layout and manually create a button to close the ad

Example:

<LinearLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/adsContainer"
   ... />

   <Button
       android:id="@+id/closeAd"
       android:text="X"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

Ai declares everything in Java too, along with Adview:

LinearLayout adscontainer = (LinearLayout) findViewById(R.id.adsContainer);
AdView mAdView = (AdView) findViewById(R.id.adView);
Button closeAd = (Button) findViewById(R.id.closeAd);

And then when the user clicks the closeAd button:

closeAd.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {

        adscontainer.removeView(mAdView);

     }
 });
  • I’ll test and you give the result!

  • 2

    It worked! Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.