0

I want to create a popup window for my app that will be the same size as my buttons which are 300dp wide. However the constructor for the popup window takes float values.

mPopupWindow = new PopupWindow(
            PopUpView,
            300,
            500
    );

Is there a way to create a popup window that will stick to dp dimensions

Brooklyn
  • 73
  • 1
  • 12

2 Answers2

0

One approach would be to obtain the size of your button in terms of pixels, using getWidth() and getHeight(), like so:

mPopupWindow = new PopupWindow(
    PopUpView,
    mButton.getWidth(),
    mButton.getHeight()
);

Be sure to call this after the layout has been inflated, or else getWidth() and getHeight() will return 0.

Another way is to take your desired value in dp and convert it to pixels yourself, then feed that into the PopupWindow constructor. Read more on this method here.

Danny Buonocore
  • 3,731
  • 3
  • 24
  • 46
0

If your aim is to convert any dp value to it's current device pixel value use below Api for conversion.

val displayMetrics = context.resources.displayMetrics
val dpValue1 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300f, displayMetrics)
val dpValue2 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 500f, displayMetrics)

mPopupWindow = new PopupWindow(
            PopUpView,
            dpValue1,
            dpValue2
    );
Anmol
  • 8,110
  • 9
  • 38
  • 63