简体   繁体   中英

How to Change Background Color of ADMOB Ad When No Internet Available

I have a ADMOB Ad at the bottom of my layout, but when there is no internet and the adView is set to View.INVISIBLE the background color still appears.

Here is what I have:

With Internet the ad shows inside the colored area to block out the listview item below it. Like this:

在此处输入图片说明

With the Internet not turned on I get only the background color from the ad showing:

在此处输入图片说明

What I really want to happen is this:

在此处输入图片说明

Here is my code for displaying the ad

MobileAds.initialize(this, "App Unit Id Here");
        if (isNetworkConnected() == true) {
            AdView adView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            adView.loadAd(adRequest);
        }

My code for checking if the Internet is present:

private boolean isNetworkConnected() {
        AdView adView = (AdView) findViewById(R.id.adView);
        ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
        //Set the visibility to "gone".
        //You can set visibility to gone here or when the function returns,
        //that is why there is a return false and true.
        adView.setBackgroundColor(0xFF00FF00);
        adView.setVisibility(View.GONE);
        return false;
            adView.setVisibility(View.INVISIBLE);
            return false;
        } else
            return true;
    }

XML Layout Portion for the Ads

<LinearLayout
        android:id="@+id/wrappers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="vertical">

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:clipToPadding="false"
            android:background="?attr/colorPrimary"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3736316423083905/5672164398"
            android:visibility="visible">

        </com.google.android.gms.ads.AdView>
    </LinearLayout>

Is there away to change the background color to be transparent when I am setting the adView to be invisible? I have tried several different things but they did not work, and I can't seem to find anything on the Stack about this. I find it hard to believe that no one has wanted to do something like this before.

Any assistance would be appreciated.

This is the way how you implement.

  • Just set AdListener to Adview. and in load faild just setVisibilityy GONE.

Example :-

1) First load Adview :-

AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

2) Set AdListener :-

adView.setAdListener(new AdListener() {
    private void showToast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAdLoaded() {
        showToast("Ad loaded.");
        if (adView.getVisibility() == View.GONE) {
            adView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        if (adView.getVisibility() == View.VISIBLE) {
            adView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onAdOpened() {
        showToast("Ad opened.");
    }

    @Override
    public void onAdClosed() {
        showToast("Ad closed.");
    }

    @Override
    public void onAdLeftApplication() {
        showToast("Ad left application.");
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM