AdMob adaptive ad banner takes almost half of the screen (portrait mode)

Viewed 59

AdMob adaptive ad banner takes almost half of the screen (portrait mode)

initially, everything works well, but if you wait, the ad is updated and takes more space (almost half of the screen)

I have reproduced it on Huawei Y6 Android 6

Maybe someone came across this? How can this be fixed? I have tried many options already.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
<FrameLayout
        android:id="@+id/frame_layout_admob_banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:maxHeight="110dp"
        android:minHeight="32dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
void setupAdViewIfNotPaid() {
    if (...) {
         return;
    }
    ConstraintLayout root = findViewById(R.id.rootView);
    int screenHeightInDp = getScreenHeightInDP(this);
    int bannerHeightInDp = getMaxAdViewHeightInDP(screenHeightInDp);
    root.findViewById(R.id.space_bottom).getLayoutParams().height = dpToPx(bannerHeightInDp);
    loadAdMobBannerAds(bannerHeightInDp);
}
private void loadAdMobBannerAds(int maxBannerHeightInDp) {
    ConstraintLayout root = findViewById(R.id.rootView);
    FrameLayout frameLayout = root.findViewById(R.id.frame_layout_admob_banner);
    frameLayout.setVisibility(View.VISIBLE);
    final String adUnitId = Tools.getMainBannerAdUnit(this);
    mAdView = new AdView(this);
    mAdView.setId(R.id.adView);
    mAdView.setAdUnitId(adUnitId);
    frameLayout.addView(mAdView);
    mAdView.setAdSize(getAdSize(maxBannerHeightInDp));
    mAdView.loadAd(Tools.getAdRequest());
}
public static int getScreenHeightInDP(Activity activity) {
    DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();
    float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;
    return Math.round(screenHeightInDP);
}
public static int getMaxAdViewHeightInDP(int screenHeightInDp) {
    int adHeight = 0;
    if (screenHeightInDp < 400)
        adHeight = 32;
    else if (screenHeightInDp <= 720)
        adHeight = 50;
    else
        adHeight = 90;
    return adHeight;
}
private AdSize getAdSize(int maxBannerHeightInDp) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        int screenWidthDp = getResources().getConfiguration().screenWidthDp;
        return AdSize.getInlineAdaptiveBannerAdSize(screenWidthDp, maxBannerHeightInDp);
    } else {
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;
        int adWidthInDp = (int) (widthPixels / density);
        return AdSize.getInlineAdaptiveBannerAdSize(adWidthInDp, maxBannerHeightInDp);
    }
}
0 Answers
Related