I am trying to create popup menu similarly like this on click on a button view in Android using Koltin. I searched for SOF and Google didn\'t find any suggestions. Can anyone pr
Following Kotlin's nice and clean principle: You can do this as well:
1)in your .xml file: (but "onClick" in xml can be used while doing java as well)
2)in your .kt file: (using kotlin's lambda expression)
fun showPopUp(view: View) {
val popupMenu = PopupMenu(this, view)
val inflater = popupMenu.menuInflater
inflater.inflate(R.menu.header_menu, popupMenu.menu)
popupMenu.show()
popupMenu.setOnMenuItemClickListener {
when(it.itemId) {
R.id.header1 -> {
Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
}
R.id.header2 -> {
Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
}
R.id.header3 -> {
Toast.makeText(this@MainActivity, item.title, Toast.LENGTH_SHORT).show();
}
...........
}
true
}
}