I am trying to create a custom tooltip using the Alert dialog (Like a pop-up view over a bottom sheet). While doing so, I am trying to position it to a particular x and y using window attributes. I will need to measure my alert dialog's width and height.
val alertDialog = AlertDialog.Builder(parent.context)
.setView(R.layout.sample_layout)
.create()
alertDialog.show()
val alertBoxHeight = alertDialog.window?.decorView?.height
val alertBoxWidth = alertDialog.window?.decorView?.width
The above returns 0.
Even after measuring the parent layout in R.layout.sample_layout
, it returns the same
Few other cases,
alertDialog.window?.attributes.width
&alertDialog.window?.attributes.height
returns -2Measuring the content of alert dialog gives almost correct height but width is very large (greater than parent)
val alertLayout = alertDialog.findViewById<ConstraintLayout>(R.id.parentLayout) alertLayout?.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED))