Unfortunately the setVisibility(View.GONE);
+ setEnabled(false)
combo does not work universally on all android versions / devices. Depending on when do you invoke it you may end up hanged in empty screen (no NPE, just blank screen).
So far the only solution that works for me is:
For Activity:
protected void removeAdView(int adViewId) {
View view = getWindow().getDecorView();
View adView = view.findViewById(adViewId);
if (adView != null) {
ViewGroup parent = (ViewGroup) adView.getParent();
parent.removeView(adView);
parent.invalidate();
}
}
For Fragment:
protected void removeAdView(View topView, int adViewId) {
View adView = topView.findViewById(adViewId);
if (adView != null) {
ViewGroup parent = (ViewGroup) adView.getParent();
parent.removeView(adView);
parent.invalidate();
}
}
This solution is based on @Quartertone's answer but extended to be more universal (i.e. works with all ViewGroup
s not just LinearLayout
). Just put these methods in your base Activity/Fragment classes.