0

Here's my code:

val inflater = LayoutInflater.from(this)
val popupView = inflater.inflate(R.layout.small_player, null)
val popupWindow = PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT/*, focusable*/)
popupWindow.elevation = 1.0f
popupWindow.showAtLocation(window.decorView, Gravity.CENTER, 0, window.decorView.height)
val height = popupWindow.contentView.height //returns 0

I also tried with popupView.height but it returns 0 and with popupWindow.contentView.layoutParams.height that returned -1

So how can I get this height?

Filippo
  • 274
  • 1
  • 4
  • 13
  • 1
    Possible duplicate of [View's getWidth() and getHeight() returns 0](https://stackoverflow.com/questions/3591784/views-getwidth-and-getheight-returns-0) – svkaka Sep 11 '18 at 10:29

2 Answers2

0

This is a very common problem, your view isn't on the screen when you are trying to get its height

try this.

val inflater = LayoutInflater.from(this)
val popupView = inflater.inflate(R.layout.small_player, null)
val popupWindow = PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT/*, focusable*/)
popupWindow.elevation = 1.0f
popupWindow.showAtLocation(window.decorView, Gravity.CENTER, 0, window.decorView.height)

popupView.post{
    val height = popupWindow.contentView.height 
}

also check this for more View's getWidth() and getHeight() returns 0

svkaka
  • 3,942
  • 2
  • 31
  • 55
0

You can use View.getMeasuredHeight() which gives you the actual height of the view.

The values you're getting now are the numerical representations used for MATCH_PARENT and WRAP_CONTENT

Basil
  • 845
  • 9
  • 24