https://kotlinlang.org logo
#android
Title
# android
n

nickk

06/11/2018, 10:10 AM
I am trying to create the view for a
PopupWindow
using
anko
. However, my button handlers run as soon as the popup is presented, and the buttons do not respond to tapping. Any idea?
Copy code
kotlin
    private fun showAlert() {
        // Get the Activity root view
        val viewGroup = (findViewById<View>(android.R.id.content) as ViewGroup).getChildAt(0) as ViewGroup

        val ui = AnkoContext.create(this)

        val customView = with(ui) {
            verticalLayout {
                backgroundColor = Color.DKGRAY
                padding = dip(10)

                button("One") {
                    alert("Hi there") {
                        yesButton { }
                    }.show()

                }

                button("Two") {
                    longToast("Ooops")
                }

            }
        }

        val popup = PopupWindow(customView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)

        popup.showAtLocation(viewGroup, <http://Gravity.TOP|Gravity.TOP>, 0, 0)
    }
g

gildor

06/11/2018, 11:09 AM
I’m not really familiar with anko, but I suppose you should pass listener to
onClick
inside button
now you just call this code inside button block, check button extension function params
n

nickk

06/11/2018, 11:21 AM
You are correct. Thanks for catching this! (A good reminder to avoid coding when tired).