Bilal Umar
Bilal Umar

Reputation: 1

Hide Native AdView if not loaded

What i am trying to achieve is hide my Ad view if it is not loaded due to internet or no ad from server.

MainActivity.java

NativeExpressAdView nativeView =
            (NativeExpressAdView) findViewById(R.id.natvie_ad);
    nativeView.loadAd(new AdRequest.Builder().addTestDevice("testDeviceID").build());

Layout

<com.google.android.gms.ads.NativeExpressAdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/moreView"
        android:id="@+id/natvie_ad"
        ads:adSize="300x80"
        ads:adUnitId="adUnitID"
        android:layout_centerHorizontal="true"

        />

Upvotes: 0

Views: 1421

Answers (1)

Ahmed Ezzat
Ahmed Ezzat

Reputation: 498

first set the visibility to gone in your xml

<com.google.android.gms.ads.NativeExpressAdView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/moreView"
    android:id="@+id/natvie_ad"
    ads:adSize="300x80"
    ads:adUnitId="adUnitID"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    />

then in your java code set an AdListener like this

NativeExpressAdView nativeView =
        (NativeExpressAdView) findViewById(R.id.natvie_ad);
nativeView.loadAd(new AdRequest.Builder().addTestDevice("testDeviceID").build());

nativeView.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
            adView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int error) {
            adView.setVisibility(View.GONE);
        }

    });

I hope that help :)

Upvotes: 4

Related Questions