Note that there are some explanatory texts on larger screens.

plurals
  1. POHide Items in Context Action Bar (CAB) dynamically when multiple items are selected
    primarykey
    data
    text
    <p>I have a listActivity that shows CAB on long click. If more than 1 item is selected I would like to hide one of my menu items. </p> <p>I keep track of the # of items selected in <code>onItemCheckedStateChanged()</code>. However I don't have access to the menu to remove the item from this function. See comments in code below to get an idea of what I was trying. I feel like I am missing some simple core understanding... code below is called from my <code>onCreate()</code> function.</p> <pre><code>private void setupActionBarContext() { ListView listView = getListView(); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { private int selCount = 0; ArrayList&lt;Long&gt; idList = new ArrayList&lt;Long&gt;(); @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { if (checked) { selCount++; idList.add(id); } else { selCount--; idList.remove(id); } mode.setTitle(selCount + " selected"); // I WOULD LIKE TO HIDE ITEM ON MENU IF 'selCount' IS &gt; 1 // For example something like this... // if (selCount &gt; 1) { // MenuItem item = menu.findItem(R.id.edit_item); // item.setVisible(false); // } else { // MenuItem item = menu.findItem(R.id.edit_item); // item.setVisible(false); // } } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.delete_item: for(Long i: idList){ mDbHelper.deleteItem(i); } mode.finish(); return true; case R.id.edit_item: Toast.makeText(getBaseContext(), "Edit Item", Toast.LENGTH_SHORT).show(); mode.finish(); // Action picked, so close the CAB return true; default: return false; } } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { // Inflate the menu for the CAB MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { selCount = 0; idList.clear(); } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } }); </code></pre> <p>And my menu item...</p> <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/edit_item" android:title="@string/edit_item" android:showAsAction="ifRoom" android:orderInCategory="1"/&gt; &lt;item android:id="@+id/delete_item" android:title="@string/delete_item" android:icon="@drawable/ic_action_delete" android:showAsAction="ifRoom" android:orderInCategory="2"/&gt; &lt;/menu&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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