jeudi 12 septembre 2019

Checkbox in PopUpMenu does not check after click

I am working on a project where I need to set a checkable PopUpMenu

I created a PopUpMenu, but I am not able to click the checkbox because the menu dissapears after click and the checkbox is not checked after dissapearing.

val wrapper: Context = ContextThemeWrapper(this, R.style.popupMenuStyle)
            var popUpMenu: PopupMenu = PopupMenu(wrapper, spinnerRecipients)

            if(list_of_names.size > 0) {
                for (i in 0 until namesList.size-1) {
                    if(list_of_names[i] != null || list_of_names[i] != "") {
                        popUpMenu.menu.add(list_of_names[i])
                            .setCheckable(true)
                            .isCheckable = true
                    }
                }
            }

popUpMenu.setOnMenuItemClickListener {
                if(popUpMenu.menu.getItem(0).isChecked)
                {
                    for(i in 0 until popUpMenu.menu.size())
                    {
                        popUpMenu.menu.getItem(i).isChecked = true
                        recipientsTW.setText("<" + popUpMenu.menu.getItem(i) + ">,")
                    }
                }

                true
            }

Style file

<style name="popupMenuStyle">
        <item name="android:textColor">#000</item>
        <item name="android:itemBackground">#FFFFFF</item>
        <item name="android:divider">@drawable/list_divider</item>
        <item name="android:dividerHeight">2dp</item>
    </style>

As a result I want to be able to click on the item in the menu and the item title should be written into the TextView next to it. In my case the first item should check all items in the menu.




Aucun commentaire:

Enregistrer un commentaire