invalidateOptionsMenu does not recreate items on old APIs - java

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.

Related

how to understand onOptionItemSeleted()

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.

Android - Menu Item does not show up after recreating Main Activity

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.

Changing a set of buttons to a menu

So I have 3 different buttons on my layout. Now as I was creating my layout for phones etc. I decided that having 3 buttons on that page was a waste of space. Those buttons already have a ton of code logic behind them, to set their visibility, replace strings depending on situation etc.
My question is, is there a simple way to change them to a menu, is it possible to simply copy the layout XML I had for them on the main page, and paste it inside a menu?
That menu would open from a simple button, and then present all 3 buttons inside of the menu. So they aren't taking up space the entire time. Will this break the code I already have? Imagine I have the following code. Will it still
bLogin.setText(getResources().getString(R.string.Exit));
bLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
And so on. I'm concerned with the menus behavior, will it close when I click one of the options, will the visibility attributes mess up the men, etc.
You could add a fourth button (let's call it menuButton) that is used to show/hide the other three buttons. You can then add the OnClickListener to the menuButton and set the visibility of the other three buttons from VISIBLE to GONE and the other way around.
So:
Button menuButton = (Button)findViewById(R.id.menuButton);
menuButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (buttonsVisible) {
otherButton1.setVisibility(View.GONE);
...
} else {
...
}
}
});
You could use menu or action bar, which would simplify things for you (you wouldn't have to worry about showing/hiding your buttons, etc). Check the docs. Also, if you're on Android 3.0+, it is recommended to migrate to action bar:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Of course, some work is required, but in the end, you will have a much more flexible solution (eg. if you will later want to add another action...)

How do I delay onCreateOptionsMenu(Menu menu)?

Im a creating and options menu dynamically, the option menu depends on data that is created during the onCreate() stage.
The problem is that onCreateOptionsMenu() is called before the data is created. Is there a way to execute after onCreate() ?
Thanks
Fabii
You can use invalidateOptionsMenu(), but it requires API level 11.
Also, you can choose to override onPrepareOptionsMenu() instead, which will give you to opportunity to modify the menu before it is displayed each time.

Replacing an ActionBar menu item icon with an indeterminate ProgressBar

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

Categories