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

后端 未结 3 1301
刺人心
刺人心 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:32

    First you have to decide the ad size that you want. There are a couple of pre-defined ones.

    The easiest to work with is SMART_BANNER, it will adapt it's width to match the current screen width, and it's height will be 32, 50 or 90 dp for screen heights <=400, (>400 && <=720) or >720 dp respectively.

    Once you know the type of banner you will request, you declare it on adSize property and reserve the space for this using layout_width and layout_height properties.

    For example for an SMART_BANNER you can do something like this:

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/ad_banner_height"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="xxxxxxxxx"/>
    

    And you will set ad_banner_height to the following values:

    values/dimens/ad_banner_height --> 32dp
    values-h400dp/dimens/ad_banner_height --> 50dp
    values-h720dp/dimens/ad_banner_height --> 90dp
    

    For other banner sizes, check: https://developers.google.com/admob/android/banner#banner_sizes

    0 讨论(0)
  • 2021-02-05 15:37

    Have you tried:

     <com.google.ads.AdView android:id="@+id/adview" 
                         android:layout_width="320dip" 
                         android:layout_height="50dip" 
                         ads:adUnitId="xxxxxxxxxxx" 
                         ads:adSize="BANNER"/> 
    

    I use AdWhirl to serve my ads for me and the way I have it is with a fixed view similar to this with fixed dimensions. Looks like you have it set to wrap content making the view disappear when nothing's there. Try it out and let me know how it works out.

    0 讨论(0)
  • 2021-02-05 15:50

    In my Banner design:

    <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:visibility="gone"
            app:adSize="BANNER"
            app:adUnitId="ca-app-pub-3940256099942544/6300978111"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </com.google.android.gms.ads.AdView>
    

    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

    0 讨论(0)
提交回复
热议问题