The menu item disappear from the activity? - android

I am trying to add menu item and it's icon, but the design result does not show the item's title and icon, What should I do?

Use this in your class and see again :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}

Related

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);

Menu return null in android

Hi this is my first question. i am learning android. here i am trying to setup menu icon top menubar.
I have added sets of item in menu. i want to manage icon from activity.
I am trying to show hide menu icon.
Menu return null in onCreate.
Is there any other way to manage menu icon dynamically ?
please help.
This is the activity class code snippet where i am trying to manage menu.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMenu = (Menu) findViewById(R.id.menuBar);//here Menu return null
mMenuItem = mMenu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
}
will appreciate your help. thanks.
First inflate menu to get MenuItem in activity method onCreateOptionsMenu and then try to get menu.getItemlike this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_activity, menu);
MenuItem item=menu.getItem(2);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setVisible(true);
return true;
}
Dont put it on your onCreate because your menu is initialized in onCreateOptionsMenu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
mMenuItem = menu.getItem(2);
mMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mMenuItem.setVisible(true);
return true;
}
Just follow the bellow instruction:
Step 1:
You need to inflate menu item from onCreateOptionsMenu(Menu menu)
Step 2:
You need a MenuInflater object that you can get using getMenuInflater()
API. Like: MenuInflater inflater = getMenuInflater();
Step 3:
Inflate you menu xml file like: inflater.inflate(R.menu.menu_bottom_nav,
menu);
Step 4:
You have to get menu object for specific item like : MenuItem menuItem =
menu.getItem(index). Here Index is the number depending on which menu item's
object you want to get.
Full code example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_bottom_nav, menu);
MenuItem menuItem = menu.getItem(0);
return true;
}

Menu Item null--->NullPointerException

I would put a number on the button that represent the numbers of notify like facebooks app.
I follow the first example in this topic :
Actionbar notification count icon (badge) like Google has
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.badge);
MenuItemCompat.setActionView(item, R.layout.feed_update_count);
notifCount = (Button) MenuItemCompat.getActionView(item);
notifCount.setText(String.valueOf(SingletonGrafica.getNumeroNotifica()));
return super.onCreateOptionsMenu(menu);
}
When I call :
MenuItem item = menu.findItem(R.id.badge);
I obtain NullPointer Exception.Anyone can help me?
You need to create the menu first:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the navigation_menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return true;
}
and if you want to update the menu you should do it in:
onPrepareOptionsMenu
if you want the onPrepareOptionsMenu function to be called again you can call invalidateOptionsMenu();
You can also add item from code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("Badge");MenuItemCompat.setActionView(item, R.layout.feed_update_count);
return true;
}

Remove Action Button From Action Bar

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();

Android - Add Accessibility to the Menu Button

I need to add accessibility to the menu button (physical menu button or non) How can I add accessibility to this component?
Something in here?
public boolean onMenuOpened(int featureId, Menu menu) {
onCreateOptionsMenu(menu);
return super.onMenuOpened(featureId, menu);
}
Thanks!
As stated by CommonsWare in the comments you just have to setTitle on your MenuItem entries. For example during inflation like:
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
menu.getItem(0).setTitle(R.string.first_label_for_menu_item);
menu.getItem(1).setTitle(R.string.second_label_for_menu_item);
}

Categories

Resources