There is a button in my application, I want to do that if someone click on this button, the menu will show up.
It will be like this menu on the first picture. How to do this?
This is how I implemented showPopUp()
function in Kotlin and I followed the same documentation that @umesh mentioned: http://developer.android.com/guide/topics./ui/menus.html#PopupMenu. Then you can call the function in your onClick()
function.
private fun showPopup(v: View) {
PopupMenu(this, v).apply {
setOnMenuItemClickListener(object: PopupMenu.OnMenuItemClickListener {
override fun onMenuItemClick(item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.settings -> {
dosomething()
true
}
else -> false
}
}
})
inflate(R.menu.menu)
show()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With