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?