How to enable/disable MenuItem based on change in class variable - android

I have an options menu in an Activity with three MenuItems
create
select
delete
When there is no item currently selected I disable the MenuItem for delete.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.basic_menu, menu);
this.deleteCharMenu = menu.findItem(R.id.basic_menu_deletechar);
this.deleteCharMenu.setEnabled(this.current != null);
return true;
}
However onCreateOptionsMenu is not called every time the menu is opened (try and error got me this result).
So I stored the MenuItem in a class variable to be able to change its state whenever this.current changed its value.
Is there a more elegant method to ensure that the correct MenuItems are enabled and disabled? I am hoping for some kind of listener I can use?

overload the onPrepareMenu , it is called right before it's shown.

Related

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.

Android: Change menu item from Log in to Log out

My menu has a item to Log in, but when you are logged in I want it to say Log out.
How?
If I'm going to change the item after its created, its probably through this method
#Override
public boolean onCreateOptionsMenu( Menu menu ) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu_main, menu );
return true;
}
Afaik the onCreateOptionsMenu() happens after the onCreate so putting any getItemId() for the menu there will give me a NullPointerException right away.
I want the app to find out if its supposed to use the string R.string.Logout if its logged in.
I dont even know what to search for for this issue. All I found was how to make a string implement names, like this answer https://stackoverflow.com/a/7646689/3064486
You should use onPrepareOptionsMenu(Menu menu) instead to update menu items
#Override
public boolean onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem someMenuItem = menu.findItem(R.id.some_menu_item);
someMenuItem.setTitle("Log out");
return super.onPrepareOptionsMenu(menu);
}
To refresh Menu items call invalidateOptionsMenu();from Activity
From Android API guides: "If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)"
After you inflate a menu, you can customize its items. To get each one, you must call findItem() with the item's id. In particular, you can use setTitle() to change the displayed string.
For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if (mIsLoggedIn)
menu.findItem(R.id.action_login).setTitle("Log out");
return true;
}
where action_login is the id you set for this particular menu item in the menu's xml file.
private void updateUI() {
runOnUiThread(new Runnable() {
#Override
public void run() {
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
MenuItem nav_signin = menu.findItem(R.id.nav_signin);
nav_signin.setTitle(MyApp.signedIn ? "Sign out" : "Sign in");
}
});
}

Verify MenuItem title in test

In my activity test I need to verify that a menu item has certain title.
I can get it by ID but it returns a view.
final View menuItem = (View) mActivity.findViewById(R.id.do_it);
But how can I get the title from this view then?
Note: I need to do it in ActivityInstrumentationTestCase2 test.
Update
I am hoping to find a way to get menu item's title in my test case without writing any additional code in activity, like storing menu in a variable.
Use getTitle():
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.<manuLayout>, menu);
MenuItem menuItem = menu.findItem(R.id.do_it);
String title = menuItem.getTitle();
return true;
}

Android - Populate menu upon activity onCreate

Perhaps I'm missing the obvious, but my menu is only been created and populated when onCreateOptionsMenu(Menu menu) is called, as in this example, and this only seem to be called after I press the menu button.
I would like to populate the menu upon the creation of the activity, how can achieve that?
UPDATE:
Perhaps a better question would be:
How can I get the Menu instance of my Activity?
Thanks
Create a class which holds those states then set your enabled/checked etc from the properties of that class in onCreateOptionsMenu()
class MenuStates{
public static boolean userDidPressTheButton;
public static boolean serverDidRespond;
public static boolean colorWasChanged;
}
void someEventHandler(){
MenuStates.userDidPressTheButton = true;
}
void onCreateOptionsMenu(){
myCheckBox.setChecked(MenuStates.userDidPressTheButton);
}
[EDIT]
You don't say why you want to get the menu instance. One approach:
Menu optsMenu;
...
// this is called once only before the end of the Activity onCreate().
onCreateOptionsMenu(Menu menu){
opstMenu = menu;
super.onCreateOptionsMenu(menu);
}
Following that, modify your menu as you wish. Do any "as it pops" up work in onPrepareOptionsMenu().
The key understanding is the difference between onCreateOptionsMenu() and onPrepareOptionsMenu().
[MORE EDIT]
To completely control the thing yourself:
Menu optsMenu;
onCreate(){
openOptionsMenu() // the menu won't show in onCreate but onCreateOptionsMenu is shown
closeOptionsMenu()
}
onCreateOptionsMenu(Menu menu){
optsMenu = menu;
}
onPrepareOptionsMenu(Menu menu){
menu.clear();
for (int i=0;ioptsMenu.size();i++){
menu.add(optsMenu.get(i).getTitle());
}
return super.onPrepareOptionsMenu(menu);
}
From the docs
You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.
http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29

android: different menu depending on situation

hello i would like to present a different menu in my activity depending on some condition. i tried like this.
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
if (specialMode)
{
inflater.inflate(R.menu.menuA, menu);
}
else
{
inflater.inflate(R.menu.menuB, menu);
}
return true;
}
however, i always get the same menu, no matter what the value of specialMode is.
onCreateOptionsMenu is called only once ( upon first creation), try using onPrepareOptionsMenu method it get called every time menu is shown.

Categories

Resources