LongClick on subMenu item of overflow menu


I’m working on the project that has an overflow menu in the android app. in onCreateOptionsMenu() I dynamically update the menu and a submenu based on user details but the task requires me to have 2 actions with that menu button, I already have a single click via onOptionsItemSelected()

is it possible to add something like OnLongClickListener to an item of the sub-menu for the overflow menu?

In my menu XML I set the subItem like this

app:showAsAction"never" >
<menu xmlns:android"http://schemas.android.com/apk/res/android"        xmlns:HTMLCode"http://schemas.android.com/apk/res-auto" >
    <item android:id"@+id/event1"
        android:title"Event 1"
        HTMLCode:showAsAction"ifRoom|withText" />
    <item android:id"@+id/event2"
        android:title"Event 2"
        HTMLCode:showAsAction"ifRoom|withText" />
    <item android:id"@+id/event3"
        android:title"Event 3"
        HTMLCode:showAsAction"ifRoom|withText" />

then in the on onCreateOptionsMenu i attach longClick like this

    var helpMenuItem  menu.findItem(R.id.help)
helpMenuItem.subMenu?.let { subMenu ->
    val ct  subMenu.size()
    for (i in 0 until ct) {
        val subItem  subMenu.getItem(i)
        subItem.actionView?.let { view ->
            view.setOnLongClickListener {
                Log.d("Long Click Event", "I'm here!!")

The code compiles but I was never able to get onLongClick. Please help have been looking for the solution for a few days wo luck


So no one answered but I need to get it done, so I kept on digging. this is the workaround solution I came up with. Instead of keeping the toolBar menu for the submenu, I create a new dialogue, there I overwrote and add my onClick and on LongClick. which did work for me, nothing else did.

private fun displayPopupDialog() {
    val context  applicationContext
    val buds  myArray.toTypedArray<CharSequence>()
    val builder: AlertDialog.Builder  AlertDialog.Builder(this)
    builder.setTitle("Select a Buddy")
    builder.setItems(buds, object : DialogInterface.OnClickListener {
        override fun onClick(dialogInterface: DialogInterface?, item: Int) {
            //  showShortToast("Clicked on:"+buddy[item]);
            val ptcode  buds[item].toString()
                clickFromDialog("code: $ptcode")
    val ad  builder.create() //don't show dialog yet
    ad.setOnShowListener(object : DialogInterface.OnShowListener {
        override fun onShow(dialog: DialogInterface?) {
            val lv  ad.listView //this is a ListView with your "buds" in it
            lv.setOnItemLongClickListener(object : AdapterView.OnItemLongClickListener {
                override fun onItemLongClick(
                    parent: AdapterView<*>?,
                    view: View?,
                    position: Int,
                    id: Long
                ): Boolean {
                    var textOnView  (view as MaterialTextView).text
                    Log.d("Long Click!", "List Item #" + position + " $textOnView was long clicked")
                    return true

    val window: Window?  ad.getWindow()
    val wlp: LayoutParams?  window?.getAttributes()
    wlp?.gravity  Gravity.RIGHT or Gravity.TOP

private fun clickFromDialog(ptcode: String) {
    Log.d("Click!", "List Item $ptcode was clicked")

Answered By – Gino

Leave a Comment