Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android checkable context menu not remembering checked

I have a button which opens a context menu with a list of various options. Only one option can be selected at a time so I want to have a radio button next to each of them highlighting which item is currently selected. When I select an item from the context menu the radio button is selected and closes. Once I click the button to open the context menu the previously selected item is not selected.

How do I get the context menu to remember which item was previously selected?

Secondly, when the activity is first created a default option is selected. How do I set an initial default which can then be overwritten when another context menu item is selected? I can set android:checked="true" in the XML but can this be overwritten when a different item is selected?

Java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browse);
    Button buttonView = (Button) this.findViewById(R.id.button_view);
    buttonView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            registerForContextMenu(v);
            openContextMenu(v);
        }
    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(R.string.menu_title);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case(R.id.item1):
            if(item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            break;
        case(R.id.item2):
            if(item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            break;
        case(R.id.item3):
            if(item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            break;
        default:
            return super.onContextItemSelected(item);
    }
    return true;
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/context_menu" android:checkableBehavior="single">
        <item android:id="@+id/item1"
            android:title="@string/context_menu_item1" />
        <item android:id="@+id/item2"
            android:title="@string/context_menu_item2" />
        <item android:id="@+id/item3"
            android:title="@string/context_menu_item3" />
    </group>
</menu>

Any assistance would be greatly appreciated!

like image 816
amctavish Avatar asked Mar 21 '12 12:03

amctavish


1 Answers

The problem is that your items don't save the state between displaying of menu. So each time you call setChecked, it works only for the currently shown menu and resets for the next.

You should save the checked state in an external structure like a boolean array for example.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(R.string.menu_title);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);

    // loop for menu items
    for (int i = 0; i < menu.size(); ++i) {
        MenuItem mi = menu.getItem(i);
        // check the Id as you wish
        if (mi.getItemId() == R.id.item2) {
            mi.setChecked(true); 
        }
    }
}

In order to search a menu item, you can also use the findItem function:

MenuItem mi = menu.findItem(R.id.item2)
like image 88
asktomsk Avatar answered Sep 18 '22 16:09

asktomsk