I'm working on a popup to display some basic bluetooth device information using PopupWindow, but I've ran into a problem. Every touch makes the popup dissapear, while some other apps have popups that do not dissapear if you keep holding down touch, which is what I'd like to achieve.
This is my current code:
private fun showDetails() {
val inflater: LayoutInflater = activity?.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
val popupView: View = inflater.inflate(R.layout.popup_tigerbuilder_details, null)
popupView.findViewById<TextView>(R.id.tgrbdetdevice_name).text = device.name
if (Build.VERSION.SDK_INT >= 30) {
popupView.findViewById<TextView>(R.id.tgrbdetdevice_nickname).text = device.alias
popupView.findViewById<TextView>(R.id.tgrbdetdevice_nickname).visibility = View.VISIBLE
}
popupView.findViewById<TextView>(R.id.tgrbdetdevice_mac).text = device.address
// create the popup window
val width: Int = LinearLayout.LayoutParams.WRAP_CONTENT
val height: Int = LinearLayout.LayoutParams.WRAP_CONTENT
val focusable = true
val popupWindow = PopupWindow(popupView, width, height, focusable)
popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT));
popupWindow.setIgnoreCheekPress()
popupWindow.animationStyle = -1
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view?.parent as View, Gravity.CENTER, 0, 0)
popupWindow.dimBehind()
popupView.setOnClickListener {
Log.d("Fragment", it.id.toString())
}
}
/**
* Dim the background when PopupWindow shows
*/
fun PopupWindow.dimBehind() {
val container = contentView.rootView
val context = contentView.context
val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
val p = container.layoutParams as WindowManager.LayoutParams
p.flags = p.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
p.dimAmount = 0.6f
wm.updateViewLayout(container, p)
}