I am having issues with menu items in the action bar. It seems like when I set the visibility to an item to false and then recreate the activity, it doesn't show up anymore. I have the menu item in the onCreateOptionsMenu and based on the current directory that is open, it alters the visibility. It works fine, until I recreate the activity. Then it doesn't show up at all. It stays invisible.. Is there a way to manually recreate the menu too so it defaults back to what is in the menu file?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if (currentDirectory.equals("/sdcard"))
menu.findItem(R.id.itemOne).setVisible(false);
return true;
}
I think what you need here is the invalidateOptionsMenu()method. What this does is that it forces the menu to be recreated.
What you do is call this method when you want the menu to return to default and then in your activity, you override public boolean onPrepareOptionsMenu(Menu menu) and make the changes you want to occur right after the menu has been recreated.
I hope this helps. Please let me know if you need more details or help.
Related
I have a form screen that is also an information screen, but in different states. When the user enters to create I want to hide the MenuItem that assigns to delete.
How to do this without breaking the app?
I'm trying to call it like this:
val menu = findViewById<MenuItem>(R.id.deleteBarra)
Here's the docs about it, but you basically want to do your menu setup in onPrepareOptionsMenu instead of onCreateOptionsMenu. onPrepareOptionsMenu gets called every time the menu needs to be displayed (instead of once, during setup).
So you can set a boolean or whatever to say whether the item should be shown, and call invalidateOptionsMenu() to redisplay it. In prepareOptionsMenu() you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item
Under what condition you want it to be hidden, you can add it under that condition.
menu.visibility = View.INVISIBLE
or
menu.visibility = View.GONE
I am dynamically styling my inflated menu items on my app on onCreateOptionsMenu(). I have a flag which I check and if I want to style them, I proceed. If I do not want to style them, I skip the styling altogether.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
if(mStyleMenu)
styleMenu(menu);
return true;
}
I use invalidateOptionsMenu() to get rid of styling whenever I want. This works on Lollipop and higher system. The style is removed from those menu items. Though, on Kitkat and Jelly Bean (for example), the items keep the style.
I have checked the comment on the invalidateOptionsMenu() and it says the items are recreated. So, I cannot explain why this happens on old systems.
Can someone give me a tip on what is going on?
Thanks.
To modify your menu at runtime, you must implement the onPrepareOptionsMenu function. See Menus.
I am fairly new to android.Recently I have been following this tutorial about making a navigation drawer for my app. Most of it seem pretty straightforward(http://blog.teamtreehouse.com/add-navigation-drawer-android). However,I am confused about what role onOptionItemSelected plays in the activity.
if (mDrawerToggle.onOptionsItemSelected(item)) {return true;}
if I remove the code above , the navigation drawer won't show when I click the toggle icon, and I am curious what is happening behind the scene when I include it and click on the drawer toggle icon.
Also what is the difference between returning false and true...I tried it out but nothing changed.
onOptionsItemSelected() is a method found to return menu item to perform action on it, if somehow its triggered.
check out the documentation for more info.
now why the drewer is not opening when you remove this method?
because it does open it.
how!
as i mentioned earlier this method returned a menu item that you have selected in this case the drawer icon which is located on the action bar.
what is the difference between returning false and true?
here it does perform the action regardless the returned data, it actually doesn't use the returned value so that won't effect the action that have been taken.
hope this will help.
Possible way of hiding menuItem base on the screen(Activity) is in the foreground. I have four (4) menuItem and I want to show two (2) in the action bar and force 2 into the overflow menu, in some screen I want to show three (3) and have one (1) in the overflow menu,and in some screen the page tile is long I do not want it truncated but instead I want to show only one (1) menuItem in the action bar and force the other three (3) into the overflow menu.
I need a generic way to do this.
I need to do this programmatically I don't need the xml answer of IfRoom, I have a BaseActivity which extends ActionBar and I have BaseActivityHelper where I have my menu layout inflated, all my other activity extends the BaseActivity.
I would love to share my code but I'm not allowed to do so, the big question is if you are the one faced with this situation how will you do it.
The docs say:
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.)
So, you can grab your menu item in onPrepareOptionsMenu and call it's setShowAsAction(int actionEnum) with the appropriate option (SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER)
I would like to add an indeterminate progress bar to the Honeycomb ActionBar, so that any time the user presses "Refresh", the refresh icon temporarily turns into an indeterminate progress bar, until the task completes. The Email app does this already, but I can't figure out how.
Any advice?
To clarify Jon O's answer, the key is to set and unset an action view on the refresh action. This works in both ActionBarSherlock and native 4.x action bar. The following snippet will put the progress indeterminate view on top of the refresh icon, assuming the refresh menu item has ID 'refresh_option' and the replacement layout (which has a ProgressBar) is in layout 'progress_wheel':
MenuItem item = abmenu.findItem(R.id.refresh_option);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View abprogress = inflater.inflate(R.layout.progress_wheel, null);
item.setActionView(abprogress);
Unset the progress view, and the refresh icon will return to visibility:
item.setActionView(null);
See a more detailed example on github.
Hard to tell exactly how the Email app does it, but you may want to stay simple and just call setIcon with the id of a StateDrawable XML file, and then just change the state using a Timer.
To simplify larham1's answer: you don't even need to inflate new action view itself because MenuItem has the method which accepts id of action layout, so you can simply write:
item.setActionView(R.layout.progress_bar);
It turns out that Google has posted an example of doing exactly this as a part of their broader ActionBarCompat compatibility project. Have a look.
You can easily do it by:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
item.setActionView(new ProgressBar(this));
break;
}
return super.onOptionsItemSelected(item);
I'm using the code provided at the original issue here: https://github.com/JakeWharton/ActionBarSherlock/issues/425
Except for android:layout_width and android:layout_height (in the actionbar_indeterminate_progress.xml) I use 32dp; as this was the way it was done in ActionBarCompat:
http://developer.android.com/resources/samples/ActionBarCompat/res/layout-v11/actionbar_indeterminate_progress.html