How to make AdView “occupy” space even while requesting an ad? (Android)

后端 未结 3 1302
刺人心
刺人心 2021-02-05 15:04

I\'m using Google AdMob Ads SDK 4.0.4 for Android

By default, The AdView will have no size until the ad is loaded. Which could cause problem if you have buttons above or

3条回答
  •  后悔当初
    2021-02-05 15:50

    In my Banner design:

    
        
    

    If it is not premium mode I call this method

    private void showBanner(){
            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(InitializationStatus initializationStatus) {
                }
            });
    
            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
            mAdView.setVisibility(View.VISIBLE);
            mAdView.setAdListener(new AdListener(){
    
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    mAdView.setLayoutParams(layoutParams);
                    mAdView.setVisibility(View.VISIBLE);
                }
    
                @Override
                public void onAdFailedToLoad(LoadAdError loadAdError) {
                    super.onAdFailedToLoad(loadAdError);
                    mAdView.setVisibility(View.GONE);
                }
            });
        }
    

    first I show the banner occupying 60dp assigned in design, I have added a listener so that the space is hidden if the banner cannot be loaded, but if the banner is loaded it assigns in height and width WRAP_CONTENT and shows it

提交回复
热议问题