Try to search your question here, if you can't find : Ask Any Question Now ?

AdMob banner's visibility stuck to View.GONE

HomeCategory: stackoverflowAdMob banner's visibility stuck to View.GONE
rupesh asked 2 weeks ago

I’ve been having this problem for weeks now. I finally narrowed it down to this : the AdView’s visibility seems stuck to View.GONE, no matter how hard I try to set it to View.VISIBLE.

Here is my code :

public class AndroidLauncher extends AndroidApplication {

    AdView bannerView;

    RelativeLayout layout;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppLovinSdk.initializeSdk(this);
        AppLovinPrivacySettings.setHasUserConsent(false, this);

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // test APP ID

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        game = new GameClass(this);

//      // Create Layout that will call both Libgdx View and AdMod View
        layout = new RelativeLayout(this);

        // InitializeCommonRessources Libgdx View
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
//
        View gameView = initializeForView(game, config);
        layout.addView(gameView);

        // Tell Android to use this Layout
        setContentView(layout);

        setupBanner();
    }

    @Override
    public void setupBanner() {
        SetupAdMobBanner();
    }

    public void SetupAdMobBanner() {
        System.out.println("Setup AdMob BANNER");

        bannerView = new AdView(this);

        bannerView.setVisibility(View.VISIBLE); // These are my desperate attempts to make the banner visible
        System.out.println("visibility == " + bannerView.getVisibility()); // Always prints "visibility == 8" which is the value for View.GONE (View.VISIBLE is 0)

        bannerView.setAdListener(new AdListener() {

            @Override public void onAdOpened() {
                System.out.println("BANNER AD OPENED");
                Ressources.incrementBannerClicks();
            }

            @Override public void onAdClosed() {
                System.out.println("BANNER AD CLOSED");
            }

            @Override public void onAdLeftApplication() {
                System.out.println("BANNER LEFT APP");
            }

            @Override public void onAdFailedToLoad(int var1) {
                System.out.println("BANNER FAILED TO LOAD : " + var1);
                destroyAdMobBanner();
                SetupAdMobBanner();
            }

            @Override public void onAdLoaded() {
                System.out.println("BANNER LOADED");
                bannerView.setVisibility(View.VISIBLE);
                System.out.println("visibility == " + bannerView.getVisibility());
            }

            @Override public void onAdImpression() {
                System.out.println("BANNER IMPRESSION");
            }
        });

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        bannerView.setAdSize(AdSize.SMART_BANNER);

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        bannerView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Test banners

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_TOP);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        layout.addView(bannerView, adParams);

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());

        loadAdMobBanner();

        bannerView.setVisibility(View.VISIBLE);
        System.out.println("visibility == " + bannerView.getVisibility());
    }

    public void loadAdMobBanner() {
        System.out.println("BANNER loadAd(adRequest)");
        AdRequest adRequest = new AdRequest.Builder().addTestDevice("36429449DBD95B918CFD96E610995AE6").build(); // Test ads for my Honor 7x
        bannerView.loadAd(adRequest);
    }

    public void destroyAdMobBanner() {
        if (bannerView == null) return;

        System.out.println("Destroying BANNER");

        layout.removeView(bannerView);
        bannerView.destroy();
        bannerView = null;
    }
}

As you can see, I am desperately trying to set the visibility to VISIBLE, but it always prints visibility == 8 right after (8 being the value for GONE, VISIBLE’s being 0).

The weirdest thing is that sometimes, the banner will actually show up.
It might work for a day or two, and suddenly not work for a day or two, the banner won’t show even though the AdListener says it’s been successfully loaded, without me doing a damn thing.

The problem occurs on a physical device, a Honor 7X, with both live ads and test ads.

1 Answers
Best Answer
Matthias answered 2 weeks ago
Your Answer

8 + 7 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com