Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MenuItem setChecked() not working

I have a menu item for sorting. When the user clicks that, a PopupMenu with the sorting options are created.

Now I have created RadioButtons for each of the items but there seems to be no way to set the selected radio button to checked state. I don't know what is going wrong.

Here is my menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/group"
        android:checkableBehavior="single">
        <item
            android:id="@+id/fileName"
            android:title="Name" />

        <item
            android:id="@+id/fileDate"
            android:title="Date" />

    </group>

</menu>

This is what I have in onOptionsItemSelected()

if (id == R.id.sort) {

            final PopupMenu popupMenu = new PopupMenu(getActivity(), view);
            popupMenu.getMenuInflater().inflate(R.menu.sort_menu, popupMenu.getMenu());

            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                public boolean onMenuItemClick(MenuItem item) {

                    int id = item.getItemId();

                    if (id == R.id.fileName) {
                        sortOrder = 0;

                    } else if (id == R.id.fileDate) {
                        sortOrder = 1;

                    } else if (id == R.id.fileSizeInc) {
                        sortOrder = 2;

                    } else if (id == R.id.fileSizeDec) {
                        sortOrder = 3;

                    }

                    item.setChecked(!item.isChecked());

                    return true;
                }
            });

            popupMenu.show();

        }

Can anyone help me solve it?

UPDATE

I realized the mistake from @gfpacheco answer. I need to do it after I show the popup menu and not before it. But the problem is how can I get the particular clicked MenuItem so that I can check it programmatically outside the callback?

like image 208
Aritra Roy Avatar asked Dec 14 '25 02:12

Aritra Roy


1 Answers

First, you should have a field to hold the current sort order, probably with a default value.

Second, before calling popupMenu.show() you should set the respective radio button checked state:

MenuItem menuItem;
switch (sortOrder) {
  case 0:
    menuItem = popupMenu.getMenu().findItem(R.id.menu_item_0);
    break;
  case 1:
    menuItem = popupMenu.getMenu().findItem(R.id.menu_item_1);
    break;
  case 2:
    menuItem = popupMenu.getMenu().findItem(R.id.menu_item_2);
    break;
}
menuItem.setChecked();

Third, update the value for the current sort order inside menu click callback:

sortOrder = newSortOrder;

This way when the popup is opened again the second step will make sure the current sort order is already checked.

like image 139
gfpacheco Avatar answered Dec 16 '25 20:12

gfpacheco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!