Naseeb Sheoran
Naseeb Sheoran

Reputation: 473

Difficulty in hiding ad when the internet connection is not available

I am making an application in which i am providing an ad.I want to hide the ad when the internet connection is not available.But i was not able to do that.This is the code which i used to achieve that result.Please tell me what is the problem with the code.

AdView myAddView = (AdView) findViewById(R.id.ad);
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            myAddView.setVisibility(8);
        } 

Upvotes: 0

Views: 70

Answers (1)

JaLoveAst1k
JaLoveAst1k

Reputation: 511

Add:

|| !ni.isAvailable() || !ni.isConnected()

Second reason: you can hide container of Ad ( for example), because it can force show himself, but can't show it's parent.

And use View.GONE instead of hardcoded "8".

Sorry for bad english.

Upvotes: 2

Related Questions