2

I want to use the newly introduced Google AdMob Adaptive Banners in my app. The quickstart illustrates a function to dynamically calculate the AdSize by DisplayMetrics.

private AdSize getAdSize() {
    // Step 2 - Determine the screen width (less decorations) to use for the ad width.
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float widthPixels = outMetrics.widthPixels;
    float density = outMetrics.density;

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

    // Step 3 - Get adaptive ad size and return for setting on the ad view.
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}

But this function uses getWindowManager() of Activity for calculation. But I just pass Context to my Class. Anyway to get the DisplayMetrics with Context?

S. Gissel
  • 1,788
  • 2
  • 15
  • 32

2 Answers2

4

Easily accomplished.

This bit

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);

can be replaced with this:

DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
S. Gissel
  • 1,788
  • 2
  • 15
  • 32
0

If are you using Cordova can use this:

this.cordova.getActivity().getWindowManager().getDefaultDisplay();