zawadz88 / MaterialPopupMenu

Shows Material popup menus grouped in sections & more
Apache License 2.0
645 stars 57 forks source link

Selected (Focused) Item #39

Closed dipcore closed 5 years ago

dipcore commented 5 years ago

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
---------
zawadz88 commented 5 years ago

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!

dipcore commented 5 years ago

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