Action Mode modify the action menu dynamically - android

I extend AbsListView.MultiChoiceModeListener for multi selection in ListView, I want to change the action menu dynamically (when more than one ListView item selected).
private class ModeCallback implements ListView.MultiChoiceModeListener {
//inflate menu
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.compose_multi_select_menu, menu);
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
//want to remove some menu here, but not work
if (getListView().getCheckedItemCount() > 1) {
MenuItem item = menu.getItem(5);
menu.removeItem();
}
}
}
I try to remove MenuItem in onPrepareActionMode(), but not work. Also tried mode.invalidate() in onItemCheckedStateChanged().
Actually, I find in onPrepareActionMode() the menu passed in have no MenuItem at all.
Anyone can help on this ?

You can modify menu in your onItemCheckedStateChanged() by showing or hiding items like this:
Menu menu = mode.getMenu();
menu.findItem(R.id.some_item_id).setVisible(false);
where mode is ActionMode passed to onItemCheckedStateChanged

Related

MenuItem text and icon are not being set

my application provides a list of recipes
at first all recipes are meat based and on the click of a switch in the setting page they become vegetable based
i want my app to change the titles and icons of the menu from meat based to vegetable based
i have put this code in the onPrepareOptionsMenu and it is being called but the titles are not being updated
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_drawer, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT > 11) {
invalidateOptionsMenu();
MenuItem item1 = menu.findItem(R.id.nav_all);
MenuItem item2 = menu.findItem(R.id.nav_beef);
MenuItem item4 = menu.findItem(R.id.nav_chicken);
Log.d("ISVEG", MeApplication.getIsVeg().toString());
if (MeApplication.getIsVeg()) {
item1.setTitle("Omlets");
item1.setIcon(ContextCompat.getDrawable(this, R.drawable.eggs));
item2.setTitle("Broccoli");
item2.setIcon(ContextCompat.getDrawable(this, R.drawable.broccoli));
item4.setTitle("Tomato");
item4.setIcon(ContextCompat.getDrawable(this, R.drawable.tomato));
} else {
item1.setTitle("All meat");
item1.setIcon(ContextCompat.getDrawable(this, R.drawable.allmeat));
item2.setTitle("Beef");
item2.setIcon(ContextCompat.getDrawable(this, R.drawable.steak));
item4.setTitle("Chicken");
item4.setIcon(ContextCompat.getDrawable(this, R.drawable.thanksgiving));
}
}
return super.onPrepareOptionsMenu(menu);
}
Remove invalidateOptionsMenu(); from onPrepareOptionsMenu. Its not required since onPrepareOptionsMenu will prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown

Change the icon to Toolbar Android

How can I get an icon from the toolbar to be changed to a new one that I get with a method that is seen in a bbdd.
The problem is that I can not access the event that updates the activity to be able to change the icon.
I tried with the onPrepareOptionsMenu method, but I can not make it work.
I have not been able to do this by putting the code in onStart because it tells me that the menu object is empty or invalid.
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
Drawable iconoMenu = obtenerIconoMenuCarro();
getMenuInflater().inflate(R.menu.menu_categorias, menu);
menu.getItem(0).setIcon(iconoMenu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_categorias, menu);
Drawable iconoMenu = obtenerIconoMenuCarro();
menu.getItem(0).setIcon(iconoMenu);
return true;
}
My activities are extended by AppCompactActivity and loaded through an AdapterView.
And I have the problem when I go back to the fragmentDialog or since the next activity.
Thanks.
For me the simplest way was to keep a reference to the MenuItem for later use.
Obtain when the menu item is inflated.
MenuItem menuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
//find the menu item from the id
menuItem = menu.findItem(R.id.myMenuItem);
return true;
}
Then change the image where you need to with mipmap resource or #drawable.
menuItem.setIcon(R.mipmap.ic_other_icon);

Android: Can't toggle MenuItem icon

I'm tring to change a menuitem icon, but the icon is not being changed.
Here is how I find the MenuItem (works fine):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_location_actions, menu);
mActionLocation = menu.findItem(R.id.action_location);
return super.onCreateOptionsMenu(menu);
}
Method UpdateIcon: I took a screenshot so you can see (in the red frame) that the images have been found by the system.
The mActionLocation is a MenuItem which is initialized before this Method is called and is not null.
Anyone has an idea?
UPDATE (solution by help from #vinitius)
The UpdateIcon method:
private void UpdateIcon(boolean locationOn) {
mActionLocation = locationOn; // mActionLocation is a global boolean
invalidateOptionsMenu();
}
The onPrepareOptionsMenu override:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Toggle location icon
if (mActionLocation) {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_on_white_24dp);
} else {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_off_white_24dp);
}
return super.onPrepareOptionsMenu(menu);
}
You need to use onPrepareOptionsMenu(Menu menu):
This is called right before the menu is shown, every time it is shown.You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
Inside this method, retrieve your item as you do in onCreateOtionsMenu and check whether your gps in os or not to modify your item icon.

Collapsed MenuItem never expands in ActionMode

I have an Item in my Action Bar (ActionBarCompat) that appears in Action mode and should display a Spinner with some options.
The code looks like:
<item
android:id="#+id/edit_context_actions"
android:title="#string/edit_context_actions"
android:icon="#drawable/ic_action_actions"
glarm:actionViewClass="android.widget.Spinner"
android:visible="true"
glarm:showAsAction="ifRoom|collapseActionView"/>
The Spinner is added in the following way:
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.edit_context_menu, menu);
// from http://stackoverflow.com/questions/11377760/adding-spinner-to-actionbar-not-navigation/11720767#11720767
Spinner actionSpinner = (Spinner) MenuItemCompat.getActionView(menu.findItem(R.id.edit_context_actions));
actionSpinner.setAdapter(new ActionSpinnerAdapterImpl2(getActivity(), mActionList));
actionSpinner.setOnItemSelectedListener(new OnActionSelectionListener());
return true;
}
The problem is that when I click on the collapsed icon, the Spinner never expands.
What am I doing wrong?

Android: Append not shown ActionItems to Icon instead of Context Menu

I found an example how to use a context menu with actionBar. This example waits for clicks on the phones menu button. But I want to have it appended to the icon or better the activity name. thanks
public class menu extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_fragen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
..
After the logo and title comes an drop down menu indicator.
That has nothing to do with onCreateOptionsMenu() or onOptionsItemSelected(). To set up that Spinner:
Get your ActionBar via getActionBar()
Call setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) on the ActionBar
Call setListNavigationCallbacks() on the ActionBar with your SpinnerAdapter and a listener object to be notified when the user makes a selection change in the Spinner

Categories

Resources