I want to create my action menu items in the ActionBar totally dinamically for some reasons. But when I add the menu items from code, they are displayed as overflow of the setting menu item.
Below there is my code. any solution?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.start, menu);
MenuItem logoutMI= menu.add(0,1,0,"Logout");
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem configMI= menu.add(0,2,1,"Configuration");
configMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
configMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
I think you need to OR those flag values together on setShowAsAction.
From the docs, http://developer.android.com/reference/android/view/MenuItem.html#setShowAsAction(int)
One of SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or
SHOW_AS_ACTION_NEVER should be used, and you may optionally OR the
value with SHOW_AS_ACTION_WITH_TEXT. SHOW_AS_ACTION_WITH_TEXT
Ex.
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
Let me know if this actually fixed your problem.
Take a look at the order field of your other menu items, you are adding "Logout" and "Configuration" with an order of 0, but if all your other menu items have an order of 0, they will be ordered based on when they were added to the menu.
Also, you will want to call setShowAsAction() only once, with an or operator:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.start, menu);
MenuItem logoutMI= menu.add(0,1,0,"Logout");
logoutMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem configMI= menu.add(0,2,0,"Configuration");
configMI.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return true;
}
Related
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
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);
I want to hook this menu on a button click listner, is it possible to do so and is it possible to change the color of the background of options menu? Thanks in anticipation
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.add(0, 1, Menu.NONE, "First");
menu.add(0, 2, Menu.NONE, "Second");
return true;
}
You should be using a combination of these two functions.
invalidateOptionsMenu() and
onPrepareOptionsMenu()
you should override onPrepareOptionsMenu() and in your button click listener call invalidateOptionsMenu().
You can also use openOptionsMenu() to programmatically open the options menu.
I want to display one image in the Right most corner of action bar. I want this image to be set at runtime depending on some code checks. Can someone guide me how to go about it?
Thanks
Use this. This code adds a refresh button at top right with the icon called R.drawable.refresh. Will show the icon only if there's enough space, else it will show it on the menu
private Menu myMenu
public void changeMenu (int resource) {
myMenu.getItem(0).setIcon(resource); //here resource is your R.drawable.insertid
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
myMenu = menu;
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
menu.add("Refresh").setIcon(R.drawable.refresh)
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
doStuff();
return false;
}
}).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return super.onCreateOptionsMenu(menu);
}
Just call changeMenu wherever you want to change the code and pass the drawable id.
Edit: Paste this on your activity and add the imports that ask you. You should add your own drawable according to what you want. Remember that icons measure
mdpi: 32x32
hdpi: 48x48
xhdpi: 64x64
if you want to change it at run time you need to make the menu (in onCreateOptionsMenu) and save the menu item. Like this
MenuItem changeableMenuItem = menu.add("new Item");
now you can call changeableMenuItem.setIcon to change the icon, then if you make changeableMenuItem a field in the class you can change the Icon anywhere in the class.
you can reference Dante's answer if you're not sure how to make the menu item.
I am created a menu item using java not xml on my actionbar which contains sub-menu as well. I am wondering is there any way i could move to the extreme left
JAVA CODE TO CREATE MENU ITEM AND SUB MENU INSIDE IT
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
SubMenu subMenu = menu.addSubMenu("Navigation Menu");
subMenu.add("Library").setIcon(R.drawable.ic_action_book);
subMenu.add("On Device").setIcon(R.drawable.ic_action_tablet);
subMenu.add("My Shelf").setIcon(R.drawable.ic_action_laptop);
MenuItem subMenu1Item = subMenu.getItem();
subMenu1Item.setIcon(R.drawable.ic_action_home);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}