Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the menu list by clicking the button?

Tags:

android

menu

There is a button in my application, I want to do that if someone click on this button, the menu will show up.

enter image description here

It will be like this menu on the first picture. How to do this?

like image 819
Piotrek Avatar asked Nov 25 '12 13:11

Piotrek


1 Answers

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()
        }
    }
like image 65
ninjahoahong Avatar answered Oct 08 '22 23:10

ninjahoahong