Search code examples
androiddimensionandroid-popupwindow

In which dimension are x/y offsets specified in showAtLocation() in PopupWindow?


I'm not sure if x and y parameters should be pixels of DensityIndependentPixels.

I've tried to get this information from Android's documentation but there's no answer: showAtLocation docs

After digging deeper I've only got more questions:

The WindowManager.LayoutParams docs contains more explaination about x and y: WindowManager.LayoutParams documantation yet still not the one that I'm looking for.


Solution

  • X/Y in layout params use pixels as their unit. You can take a look at this How to show PopupWindow at special location? for more information on how-to.

    Extra: you can also convert this value with

    float density = context.getResources().getDisplayMetrics().density;
    float px = someDpValue * density;
    float dp = somePxValue / density;
    

    Extra 2: you can also get width/height of the screen

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();    
    float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;