How to change menu item text dynamically in Android
As JxDarkAngel suggested, calling this from anywhere in your Activity,
invalidateOptionsMenu();
and then overriding:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.bedSwitch);
if (item.getTitle().equals("Set to 'In bed'")) {
item.setTitle("Set to 'Out of bed'");
inBed = false;
} else {
item.setTitle("Set to 'In bed'");
inBed = true;
}
return super.onPrepareOptionsMenu(menu);
}
is a much better choice. I used the answer from https://stackoverflow.com/a/17496503/568197
Android, change title of Menu item programmatically
Because Menu's getItem() method returns a menu item at a given index as opposed to passing in a resource ID which can potentially throw an IndexOutOfBoundsException as mentioned here in the docs. Instead, you should be using index values so it'd be 0 for the first menu item, menu_1, and 1 for the other menu item, menu_2.
Referencing and changing a menu item
You are referencing the menu item from the wrong place. The following solution should work.
public class TestActivity extends AppCompatActivity {
private Menu menu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_written_note, menu);
this.menu = menu;
return true;
}
private void updateMenuTitle() {
MenuItem item = menu.findItem(R.id.itEdit);
item.setTitle("Test");
}
}
How to replace menu dynamically with another in Android
Create and prepare the options menu for changing and its item selection method
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getMenuInflater();
if(menuString=="red"){
inflater.inflate(R.menu.red_menu, menu);
}else if(menuString=="green"){
inflater.inflate(R.menu.green_menu, menu);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
return true;
case R.id.help:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Like whenever you want to change menu call
String menuString="red or green";
invalidateOptionsMenu();
Like others told, if you want to have static menu use onCreateOptionsMenu
, and if you want to change its visibility dynamically use onPrepareOptionsMenu
along with onCreateOptionsMenu
How can I dynamically create menu items?
How to Dynamically Add Menu Items to an Android Activity
public class yourActivity extends Activity {
...
private static final int MENU_ADD = Menu.FIRST;
private static final int MENU_LIST = MENU.FIRST + 1;
private static final int MENU_REFRESH = MENU.FIRST + 2;
private static final int MENU_LOGIN = MENU.FIRST + 3;
/**
* Use if your menu is static (i.e. unchanging)
*/
/*
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);
menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon);
menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon);
menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon);
return true;
}
*/
/**
* Gets called every time the user presses the menu button.
* Use if your menu is dynamic.
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(enableAdd)
menu.add(0, MENU_ADD, Menu.NONE, R.string.your-add-text).setIcon(R.drawable.your-add-icon);
if(enableList)
menu.add(0, MENU_LIST, Menu.NONE, R.string.your-list-text).setIcon(R.drawable.your-list-icon);
if(enableRefresh)
menu.add(0, MENU_REFRESH, Menu.NONE, R.string.your-refresh-text).setIcon(R.drawable.your-refresh-icon);
if(enableLogin)
menu.add(0, MENU_LOGIN, Menu.NONE, R.string.your-login-text).setIcon(R.drawable.your-login-icon);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MENU_ADD: doAddStuff(); break;
case MENU_LIST: doListStuff(); break;
case MENU_REFRESH: doRefreshStuff(); break;
case MENU_LOGIN: doLoginStuff(); break;
}
return false;
}
The following specific example adds a MENU_LOGOUT option if the user is logged in.
private static final int MENU_LOGOUT = MENU.FIRST + 4;
public boolean onPrepareOptionsMenu(Menu menu) {
...
if(auth.isLoggedIn()) {
menu.add(0, MENU_LOGOUT, Menu.NONE, R.string.your-logout-text).setIcon(R.drawable.your-logout-icon);
}
...
}
public boolean onOptionsItemSelected(MenuItem item) {
...
case MENU_LOGOUT:
if(auth.isLoggedIn()) {
doLogout();
} else {
Toast.makeText(this, "You must have somehow been logged out between the time the menu button was pressed and now.", Toast.DURATION_LONG).show();
}
break;
...
}
That's all there is to it.
Related Topics
Execute Shell Command from Android
Using Static Variables in Android
How to Add a Recyclerview Inside Another Recyclerview
Tomcat Doesn't Stop. How to Debug This
Kiosk Mode for Linux Java Swing Application
Run Custom Textsecure (Signal) Server
Execute Source Command from Java
Gson Typetoken with Dynamic Arraylist Item Type
Detecting a Long Press in Android
Java.Lang.Numberformatexception: Invalid Double: " "
Android JSONobject - How to Loop Through a Flat JSON Object to Get Each Key and Value
Problems with Java3D Lib Configuration
Why Stringbuilder Stops Adding Elements After Using the Null Character
A Simple Way of Embedding a Video in My Swing Gui
How to Include Platform-Specific Native Libraries in the .Jar File Using Eclipse