'PopupWindow showAtLocation located differently on different screen sizes

Is there any way to show PopupWindow.showAtLocation() in exact same place near view on different devices?

My show method:

fun PopupWindow.showAtLocationForEditProfile(anchor: View) {
    if (!isShowing) {
        val location = IntArray(2)

        anchor.postDelayed({
            anchor.getLocationInWindow(location)
            val isFromTop: Boolean
            val y =
                if (location[1] - anchor.context.convertDpToPixel(175f + 56) < 0
                ) {
                    isFromTop = false
                    location[1]
                } else {
                    isFromTop = true
                    location[1] - anchor.context.convertDpToPixel(24f)
                }

            showAtLocation(
                anchor,
                if (!isFromTop) Gravity.TOP else Gravity.BOTTOM,
                0,
                y
            )

        }, 300)
    }
}

fun Context.convertDpToPixel(dp: Float): Int {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        dp,
        Resources.getSystem().displayMetrics).toInt()
}

enter image description here

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source