Change menu text android - android

I have a string that i get from asynctask, i would like to set that string as title of menu item in actionbar.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
this.menu = menu;
if (menu!= null) {
menu.findItem(R.id.azzera).setVisible(false);
}
MenuItem giornataItem = menu.findItem(R.id.giornata);
giornataItem.setTitle(giornataTitle);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
MenuItem giornataItem = menu.findItem(R.id.giornata);
giornataItem.setTitle(giornataTitle);
return super.onPrepareOptionsMenu(menu);
}
OnPrepareOptionsMenu works fine, when i change fragment or drawernav is open the title is still correct, but onCreateOptionsMenu does not set the title on application start because in that moment the string is null, because it's called before asynctask.
How can i set that string as title?

call asynctask while create menu
String tital=new Asynctask().execute("").get();
MenuItem giornataItem = menu.findItem(R.id.giornata);
giornataItem.setTitle(tital);
if you are talking about making a string to tital of your activity
then call
String tital=new Asynctask().execute("").get();
setTitle(tital);
setContentView(R.layout.MainActivity);

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

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

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.

Weird behavior when calling invalidateOptionsMenu() in Android 4.3

I have a menu with three items, each one is shown alone with a screen. I call invalidateOptionMenu() in the second screen to refresh the action bar color.
The color change does work pretty well in both 4.1.2 and 4.3 versions.
But on Android 4.3, when I call this, the item of the first screen is shown on the second screen.
Here is my code this is in (#Override public void onClick(View v):
if (android.os.Build.VERSION.SDK_INT >= 11){
final ActionBar actionBar = getActionBar();
this.invalidateOptionsMenu();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(couleur)));
}
and even if I try that after it doesn't work:
final MenuItem saveNote = Menu.findItem(R.id.saveNote);
final MenuItem removeNote = Menu.findItem(R.id.deleteNote);
final MenuItem nouvelleNote = Menu.findItem(R.id.nouvelleNote);
nouvelleNote.setVisible(false); //screen 1
saveNote.setVisible(true); //screen 2
removeNote.setVisible(false); //screen 3
And here is how I override onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
Menu = menu;
getMenuInflater().inflate(R.menu.pumpkin_note, menu);
final MenuItem saveNote = Menu.findItem(R.id.saveNote);
final MenuItem removeNote = Menu.findItem(R.id.deleteNote);
final MenuItem nouvelleNote = Menu.findItem(R.id.nouvelleNote);
nouvelleNote.setVisible(true);
saveNote.setVisible(false);
removeNote.setVisible(false);
return true;
}
In onCreateOptionsMenu you should just inflate your menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
in onPrepareOptionsMenu you should change menu items visibility by some condition
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.saveNote).setVisible(isFirstScreen);
menu.findItem(R.id.deleteNote).setVisible(isSecondScreen);;
menu.findItem(R.id.nouvelleNote).setVisible(isThirdScreen);;
}
To update your menu you should call invalidateOptionMenu(), this calls onPrepareOptionsMenu and menu redraw again

Categories

Resources