Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid checkable context menu not remembering checked
    primarykey
    data
    text
    <p>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.</p> <p>How do I get the context menu to remember which item was previously selected?</p> <p>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 <code>android:checked="true"</code> in the XML but can this be overwritten when a different item is selected?</p> <p>Java:</p> <pre><code>@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; } </code></pre> <p>XML:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;group android:id="@+id/context_menu" android:checkableBehavior="single"&gt; &lt;item android:id="@+id/item1" android:title="@string/context_menu_item1" /&gt; &lt;item android:id="@+id/item2" android:title="@string/context_menu_item2" /&gt; &lt;item android:id="@+id/item3" android:title="@string/context_menu_item3" /&gt; &lt;/group&gt; &lt;/menu&gt; </code></pre> <p>Any assistance would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload