3

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?

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • @DanielNugent have mentioned that in the first line itself. – Aritra Roy Jun 30 '15 at 03:46
  • check this [post](http://stackoverflow.com/questions/30471846/group-menu-items-work-but-dont-display-checkmark/30472013#30472013). your answer is there.. also always remember to acknowledge a post – Elltz Jun 30 '15 at 03:51
  • I saw that. Its not the case here. – Aritra Roy Jun 30 '15 at 03:55
  • okay so you want to get the particular menu item that was clicked and check it outside of your menuItem interface? if that is the case you know what item takes you or does for you so if first item opens a view in that view you find your menuitem by something like `popmenu.getMenu().getItem(int)` is this what you want? – Elltz Jun 30 '15 at 04:00

3 Answers3

4

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.

gfpacheco
  • 2,831
  • 2
  • 33
  • 50
-1

here is the solution i found for two option menu items you want to wrap it with menu and group.working 100%

add this two both items android:orderInCategory="1" for 1st item and android:orderInCategory="2" for 2nd item

S Sathiya
  • 47
  • 7
-1

<item
    android:id="@+id/currency"
    android:icon="@drawable/currency"
    android:showAsAction="always"
    android:title="rthrth">
    <menu>
        <group
            android:id="@+id/currencies"
            android:checkableBehavior="single" >
            <item
                android:id="@+id/us"
                android:orderInCategory="1"
                android:showAsAction="never"
                android:title="@string/us"/>
            <item
                android:id="@+id/tr"
                android:orderInCategory="2"
                android:showAsAction="never"
                android:title="@string/tr"/>
        </group>
    </menu>
</item>

S Sathiya
  • 47
  • 7