TimWeb
TimWeb

Reputation: 405

How to get Size for Adaptive Banner (Admob) API 30

The code below is not working due to deprecated methods getDefaultDisplay(); and getMetrics() in API 30.

private AdSize getAdSize() {
    Display display = getWindowManager().getDefaultDisplay();//deprecated
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);//deprecated

    float density = outMetrics.density;
    float widthPixels = adContainerView.getWidth();

    if (widthPixels == 0) {
        widthPixels = outMetrics.widthPixels;
    }

    int adWidth = (int) (widthPixels / density);

    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}

Google requires use WindowMetrics#getBounds() to get the dimensions of the application window area, and Configuration#densityDpi to get the current density.

I found these methods for API 30 but I cannot figure out how to use them.

final WindowMetrics metrics = windowManager.getCurrentMetrics();
 // Gets all excluding insets
 final WindowInsets windowInsets = metrics.getWindowInsets();
 Insets insets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
 final DisplayCutout cutout = windowInsets.getCutout();
 if (cutout != null) {
     final Insets cutoutSafeInsets = Insets.of(cutout.getSafeInsetsLeft(), ...);
     insets = insets.max(insets, cutoutSafeInsets);
 }

 int insetsWidth = insets.right + insets.left;
 int insetsHeight = insets.top + insets.bottom;

 // Legacy size that Display#getSize reports
 final Size legacySize = new Size(metrics.getWidth() - insetsWidth,
         metrics.getHeight() - insetsHeight);

Has anyone faced this?

Upvotes: 2

Views: 1483

Answers (5)

Josef
Josef

Reputation: 11

private AdSize getAdSize() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
        Rect bounds = windowMetrics.getBounds();

        float adWidthPixels = adContainerView.getWidth();

        // If the ad hasn't been laid out, default to the full screen width.
        if (adWidthPixels == 0f) {
            adWidthPixels = bounds.width();
        }

        float density = getResources().getDisplayMetrics().density;
        int adWidth = (int) (adWidthPixels / density);

        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
    } else {
        DisplayMetrics outMetrics = getResources().getDisplayMetrics();
        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;
        int adWidth = (int) (widthPixels / density);
        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
    }
}

Upvotes: 1

Shuji Kaya
Shuji Kaya

Reputation: 21

I had same question and came here and found still it is not clearly solved. Below is my solution. It is C# Xamarin. So get* -> * difference exist. But it may be hint. I want someone to modify this right answer.

        Context     context = Android.App.Application.Context;
        IWindowManager windowManager = context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
        float   adwidth, scale;
        if ((int)Android.OS.Build.VERSION.SdkInt >= 30) {
            WindowMetrics metrics = windowManager.CurrentWindowMetrics;
            Rect    bounds = metrics.Bounds;
            adwidth = (bounds.Width() < bounds.Height() ? bounds.Width() : bounds.Height());
            scale = context.Resources.Configuration.DensityDpi / 160f;
        }
        else {
            Display     display = windowManager.DefaultDisplay;
            DisplayMetrics  displayMetrics = new DisplayMetrics();
            display.GetMetrics(displayMetrics);
            adwidth = (displayMetrics.WidthPixels < displayMetrics.HeightPixels ? displayMetrics.WidthPixels : displayMetrics.HeightPixels);
            scale = displayMetrics.Density;
        }
        mAdView.AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(this, (int)(adwidth / scale));

Upvotes: 0

Hesham Yemen
Hesham Yemen

Reputation: 872

I found the solution

  private static AdSize getAdSize(AppCompatActivity activity) {
    int windowWidth = activity.getResources().getConfiguration().screenWidthDp;
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, windowWidth);
}

Upvotes: 0

Bokili Production
Bokili Production

Reputation: 414

I use this method:

private AdSize getAdSize() {
    DisplayMetrics outMetrics = getResources().getDisplayMetrics();
    float widthPixels = outMetrics.widthPixels;
    float density = outMetrics.density;
    int adWidth = (int) (widthPixels / density);
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(requireContext(), adWidth);
}

For LinearLayout, for view the Adaptive banner:

    LinearLayout banner_view = findViewById(R.id.banner_view);
    AdView adView = new AdView(requireActivity().getApplicationContext());
    adView.setAdUnitId(getString(R.string.my_banner_id));
    banner_view.addView(adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    AdSize adSize = getAdSize(); // Code above
    adView.setAdSize(adSize);
    adView.loadAd(adRequest);

Upvotes: 2

한민우
한민우

Reputation: 1

google develop https://developer.android.com/training/multiscreen/screendensities?hl=en

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        return context.getResources().getDisplayMetrics().densityDpi;

    } else {
        DisplayMetrics displayMetrics = new DisplayMetrics();

        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int deviceWidth = displayMetrics.widthPixels;
        int deviceHeight = displayMetrics.heightPixels;

        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
        return metrics.densityDpi;
    }

Upvotes: 0

Related Questions