zawadz88/MaterialPopupMenu

Selected (Focused) Item

Closed this issue · 2 comments

Is there a way to set which item is selected/focused on menu opening?
for instance I have menu

----------
Option 1
Option 2   <- I want this item to be focused
Option 3
---------

Hi @dipcore,
I think this should be possible with a customItem and viewBoundCallback. In the sample app there's e.g.

customItem {
    layoutResId = R.layout.view_custom_item_checkable
    viewBoundCallback = { view ->
        val checkBox: CheckBox = view.findViewById(R.id.customItemCheckbox)
        checkBox.isChecked = true
    }
    callback = {
        Toast.makeText(this@LightActivity, "Disabled!", Toast.LENGTH_SHORT).show()
    }
}

so in viewBoundCallback you do sth like view.isFocused = true and check if it does the trick.

Let me know if this helped!

Hey @zawadz88
Yes, that worked. Here is my code

                        customItem {
                            layoutResId = R.layout.mpm_popup_menu_item
                            viewBoundCallback = { view ->
                                view.findViewById<TextView>(R.id.mpm_popup_menu_item_label)
                                        .text = option.name
                                if (option.name == model.value)
                                    view.post { view.requestFocus() }

                            }
                            callback = {
                                ...
                            }
                        }

So I have to use view.post { view.requestFocus() } to make it work properly.

Thanks for your help