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;
}
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
I have implemented ActionBar Navigation using Fragment. In my App i have one Activity and rest is in Fragments. In my MainActivity i am implementing menu like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Two Fragments uses Navigation Drawer and in their respected fragments i am inflating menu buttons to sort items.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.sort_button_shops, menu);
}
Now the Problem is if i open the Fragment 1 it works perfectly. When i open fragment 2, it shows 2 button to sort, one from Fragment 1 and the second one from Fragment 2.
I have tried to hide the button but it didn't worked.
Any Help will be Appreciated.
Thanks
When you inflate a new menu you are adding new items to the old Menu object, which is probably not what you intended.
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.removeItem(R.id.your_menu_item);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Try using this in onResume() of fragments.
MenuItem item = (MenuItem) findViewById(R.menu.activity_main);
item.setVisible(false);
this.invalidateOptionsMenu();
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 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;
}
I am using Android native action bar in my app. I created the drop down menu of action bar by:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
return true;
}
For certain page, I would like to hide the action bar drop down menu. How to achieve this? Seems there is no direct method to set visibility to false for this drop down menu...
I managed to resolve this problem myself by set each menu item in drop down menu of action bar to be invisible:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar_menu, menu);
menu.getItem(2).setVisible(false);
menu.getItem(3).setVisible(false);
return true;
}
you should try to change the navigation mode of action bar when you create the activity...
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);