16

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 provide a sample code to achieve it using kotlin.

P. Mohanta
  • 130
  • 2
  • 15
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • Kotlin is just a language. You'll find plenty examples in Java, I suggest you, at least, understand both. Once you understand what the code does, then you can play with different languages and syntaxes. – Eugen Pechanec Feb 05 '18 at 20:35
  • Yeah, I agree with you, but there are no examples using Kotlin language. – Shailendra Madda Feb 06 '18 at 02:40

3 Answers3

43

Finally, I did it, It may help someone

Step 1. First, create an activity_main.xml contains a button named my_button

Step 2. Then take header_menu.xml under res/menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/header1"
        android:title="H1" />
    <item
        android:id="@+id/header2"
        android:title="H2" />
    <item
        android:id="@+id/header3"
        android:title="H3" />

</menu>

Step 3. Finally, in MainActivity.kt use it like:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        my_button.setOnClickListener {
             showPopup(my_button)
          }
    }

    private fun showPopup(view: View) {
        val popup = PopupMenu(this, view)
        popup.inflate(R.menu.header_menu)

        popup.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item: MenuItem? ->

            when (item!!.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
        })

        popup.show()
    }
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
4

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)

<Button 
  ........
  android:onClick="showPopUp"
  ....../>

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
    }
}
Ujjwal Jung Thapa
  • 604
  • 2
  • 8
  • 31
  • Setting the method in xml only appears to work if the method is in an Activity, not a fragment. https://stackoverflow.com/a/18896666/211292 – ThomasW Jul 02 '20 at 12:48
2

for people who hate xml:

    private fun showOverflowMenu(context: Context, anchor: View) {
        val menu = PopupMenu(context, anchor)

        menu.menu.apply {
            add("Rename").setOnMenuItemClickListener {
                // TODO rename
                true
            }
            add("change context").setOnMenuItemClickListener {
                // change stuff
                true
            }

            add("delete").setOnMenuItemClickListener {
                // TODO delete some stuff
                true
            }
        }

        menu.show()
    }
George Shalvashvili
  • 1,263
  • 13
  • 21