I see the following misfeature on Android 4.0.4, HTC T-Mobile:
My program changes the options menu at run-time (namely, things like replacing a "Start" button with a "Stop" button). Unfortunately, the overflow menu disappears, and at least one menu item is "lost". On the first sight, it looks like the "More" menu item is replaced by another menu item, "Settings" in my case.
This does not happen on Android 2.
(I found a workaround and am posting this in case somebody else encounters this problem)
The workaround is not to let the number of visible items to decrease. It looks like Android 4 removes the "More" item when it's not needed but cannot add it when it is needed again.
I have changed my code from
private void doPrepareOptionsMenu(Menu menu) {
boolean running = ...;
menu.findItem(R.id.menu_stop).setVisible(running);
menu.findItem(R.id.menu_start).setVisible(!running);
}
to
private void doPrepareOptionsMenu(Menu menu) {
boolean running = ...;
if (running) {
menu.findItem(R.id.menu_stop).setVisible(running);
menu.findItem(R.id.menu_start).setVisible(!running);
} else {
menu.findItem(R.id.menu_start).setVisible(!running);
menu.findItem(R.id.menu_stop).setVisible(running);
}
}
and the problem disappeared. The difference is that we first make an item visible and only then make another item invisible.
In case you never changed the Android 2 menu at run-time, some context how it works:
SomeListener someListener = new SomeListener() {
public void someStateChanged() {
// This runs NOT on the UI thread
runOnUiThread(new Runnable() {
public void run() {
updateUI();
}
});
}
}
and updateUI() finally calls the code
Menu menu = weakRefOptionsMenu.get();
if (null != menu) {
doPrepareOptionsMenu(menu);
}
and, of course, onPrepareOptionsMenu() also calls doPrepareOptionsMenu():
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
doPrepareOptionsMenu(menu);
return true;
}
Related to the original issue is a case where you have a single menu item that by default is invisible. If you plan on making this item visible at runtime based on some logic, it will not appear on 4.0.4 devices. The fix is to include a disabled, empty-titled, visible item:
<item
android:title=""
android:visible="true"
android:enabled="false"
android:showAsAction="ifRoom"/>
<item
android:id="#+id/my_item"
android:title="My Item"
android:visible="false"
android:showAsAction="ifRoom"/>
Related
In short, here's my question:
Can option menus (shown in the actionbar) be modified programatically on android 3.0+?
I have a wizard-style activity in which I use a ViewFlipper to switch between views, or steps.
The steps are: 1 -> 2 -> 3. Only the second screen (2) has a menu item, while the others don't. I have tried hanging on to the Menu reference (source) and either removing/adding items or just hiding/showing them.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
mMenu.getItem(0).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Switch to the second screen ->
public void showNext(View v) {
if (mVFlipper.getDisplayedChild() < (mVFlipper.getChildCount() - 1)) {
mVFlipper.showNext();
if (mVFlipper.getDisplayedChild() == 1) {
setTitle("Second screen");
mMenu.getItem(0).setVisible(true);
}
}
}
This works fine on 2.2, but fails miserably on 4.1. Starting off with a visible MenuItem and hiding it later works. Starting off with an invisible menu item and showing it later -
There is a bug in Android's MenuItem setVisible that causes problems when turning items back to visible.
In your onCreateOptionsMenu(), add a check to see if the displayed page needs the Menu, if it does, add the MenuItem. Then, call invalidateOptionsMenu() whenever the page changes. That will rebuild the Menu.
I was also struggling with this issue, then I applied a small hack:
menu1.setEnabled(false);
menu1.setTitle("");
Then where you want to visible it again:
menu1.setEnabled(true);
menu1.setTitle("Okay"); //or you can set text according to your given updated values.
Problem fixed by having the MenuItem be visible after onCreateOptionsMenu finishes and then hiding it from a callback called after onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
boolean dummyVal = super.onCreateOptionsMenu(menu);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
mMenu.getItem(0).setVisible(true);
} else {
mMenu.getItem(0).setVisible(false);
}
return dummyVal;
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (mMenu != null) {
mMenu.getItem(0).setVisible(false);
}
}
}
If anyone has this problem, I recommend trying toadzky's suggestion first: calling "invalidateOptionsMenu()".
I have a basic problem where an initially hidden MenuItem is unable to be toggled to visible. As a caveat, I am using ActionBarSherlock, but I wanted to see if anyone knew if this was a known issue of Android or I am doing something terrible before investigating whether this is an issue inside of ABS. Code as follows:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_xml, menu);
mMenuItem = menu.findItem(R.id.menu_item);
mMenuItem.setVisible(false);
return true;
}
// Somewhere elsewhere
// MenuItem is never visible after this line is executed
mMenuItem.setVisible(true);
I have also tried to move the mMenuItem assignment and visibility into a call to onPrepareOptionsMenu but the same behavior is shown.
Thanks!
The problem is you are not telling Android that it needs to update the menu. This drove me nuts for the last hour until I figured out a solution. I don't think it's as apparent on pre-HC because menu items aren't always visible on the screen like they are in HC+.
On your activity, simply call:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
this.invalidateOptionsMenu();
}
That will trigger a call to the onCreateOptionsMenu() event again, so if you're setting the visibility in that function for initialization you'll need to take into account then if you want the option to show or not.
I had the same problem and I found out that setVisible(true) works when there is at least another MenuItem visible. I hope this can be helpful to someone.
I understand this is old question.
But I've solved it by placing inside onPrepareOptionMenu() {...}
private boolean mShowVisible=false;
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_item).setVisible(mShowVisible);
return super.onPrepareOptionsMenu(menu);
}
whenever you want to set visible or not just call it as:
mShowVisible = true; // or false
invalidateOptionMenu();
I found that using a view's post(Runnable) method to setVisible does the trick, so something like...
view.post(new Runnable() {
#Override
public void run() {
menu.findItem(id).setVisible(true);
}
});
An Android device configuration change (for example "slide the hard keyboard back in") will always call PhoneWindow.onConfigurationChanged(), which in turn, will call reopenMenu(). This will cause the menu of the currently running activity to be reopened, in case it is showing.
I have a lock on my menu implemented in my onPrepareOptionsMenu() override. The user must enter a code each time they want to see the menu. I don't want the user to be asked to enter the code again, while the menu is still up just because of a configuration change. Thus, I would like to know, is there any way I can check if the menu of current foreground activity is already showing? Knowing this, I could bypass asking for the access code if the menu is already up.
My custom workaround implementation is to use my own flag menuShowing, which I set in onPrepareOptionsMenu and reset in onOptionsItemSelected and in onKeyDown if the back button is clicked.
EDIT: It appears a screen orientation configuration change does not trigger this behavior. A hard keyboard slide however, does.
Until someone comes up with a nicer 'one call' answer, here is the custom workaround implementation that I mention in the question, with help from Sam's tips, in case someone needs the same functionality:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (showingMenu) {
// The menu button was clicked or the hard keyboard was
// slid open/closed while the menu was already showing
return true;
}
// Otherwise, either the menu was clicked or openOptionsMenu() was called
if (codeEntered) {
// Code was entered and then openOptionsMenu() was called
showingMenu = true;
// Menu will now be shown
return true;
} else {
// The menu button was clicked, ask for code
askForCode();
// Don't show menu yet
return false;
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
showingMenu = false;
codeEntered = false;
}
private void askForCode() {
codeEntered = getUserInput();
if (codeEntered)
openOptionsMenu();
}
getUserInput() actually occurs with the help of an AlertDialog and an EditText with an attached TextWatcher but the implementation details exceed the scope of this question, unless someone is interested.
In my case it´s
#Override
public void onPanelClosed(int featureId, Menu menu) {
showingMenu = false;
super.onPanelClosed(featureId, menu);
}
Okay, so I have an option menu for my current app and it is the same with every class. okay, so I would like to know how to dim the options that are included in the menu when the selected class is already selected. For example, I have the Main Home on my option menu. When I am at the Main Home screen...it does appear on the option menu to click. How in the heck do you dim this? I tried looking on the android development page..but had no luck.. yet, I see it on other apps and it is driving me crazy! Surely, I am sure it is easy to mark the code out..but how can you do it! it is driving be bananas!
http://developer.android.com/guide/practices/ui_guidelines/menu_design.html#dim_hide_menu_items
You can override onPrepareOptionsMenu and disable the relevant menu item(s).
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean ok = super.onPrepareOptionsMenu(menu);
if (ok) {
MenuItem item = menu.findItem(id_for_this_screen);
if (item != null) {
item.setEnabled(false);
}
}
return ok;
}
so I am trying to get my menu item, that is show on the action bar to behave like a checkable menu option. The firs part works, meaning it is checkable and when I press it, and set in code the setChecked(true) it works. But what does not work is the visual part. There is no change in how a menu item looks on the action bar in checked and unchecked states? I tried using invalidateOptionsMenu() but that does not do the job, and not only that, with that line in my code I can't get out of the checked state?!?
What happens is that invalidate OptionsMenu() seams to unset the checked state and I end up 'looping', or on every press of that menu item I keep going to the unchecked part of the code where it gets checked and with invalidate it gets unchecked I guess...
Here is the code from my XML file for menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/lenslist_menu_add"
android:showAsAction="always"
android:title="#string/add"/>
<item android:id="#+id/lenslist_menu_delete"
android:showAsAction="always"
android:checkable="true"
android:title="#string/delete"/>
</menu>
And here is the java code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.lenslist_menu_add:
return true;
case R.id.lenslist_menu_delete:
if (item.isChecked() == true) {
item.setChecked(false);
deleteMode = false;
lensAdapter.setDeleteMode(false);
} else {
item.setChecked(true);
deleteMode = true;
lensAdapter.setDeleteMode(true);
}
lensAdapter.notifyDataSetChanged();
return true;
}
return super.onOptionsItemSelected(item);
}
Thanks!
Checkable items appear only in submenus or context menus.
You are using them as main menu items, hence it will not work.
SOURCE: Download the API DEMOS, and open the file ApiDemos/res/menu/checkable.xml, you'll see it as a comment on line 13. I don't know why they don't mention this in the Developer Documentation
reference with comment.:
http://alvinalexander.com/java/jwarehouse/android-examples/platforms/android-2/samples/ApiDemos/res/menu/checkable.xml.shtml
Or just do it yourself
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.item1).setIcon(menu_checked?R.drawable.menu_ico_checked:R.drawable.menu_ico_unchecked);
return super.onPrepareOptionsMenu(menu);
}
and in onOptionsItemSelected do:
....
menu_checked=!menu_checked;
invalidateOptionsMenu();
The best solution is to set the actionLayout of the <Item> to a CheckBox. This solution gives you a native-looking checkbox (with material animations etc), with a font that matches the other items, and it works both as an action and in the submenu.
Create a new layout called action_checkbox.html:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:checked="false"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Menu"
android:id="#+id/action_item_checkbox"
/>
Set your <Item> like this. Note that you need the Checkable and Checked still in case it is shown in a sub-menu (in which case the actionLayout is ignored.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/menu_action_logging"
android:title="#string/action_logging"
android:orderInCategory="100"
android:showAsAction="always"
android:checkable="true"
android:checked="false"
android:actionLayout="#layout/action_checkbox"
/>
</menu>
In your code, when the menu is created we need to a) set the title of the checkbox to match the menu item title, b) restore the checked state of both the menu checkable, and our extra checkbox, and c) add an onClicked() listener for our extra checkbox. In this code I am persisting the state of the checkbox in a RetainedFragment.
// Set the check state of an actionbar item that has its actionLayout set to a layout
// containing a checkbox with the ID action_item_checkbox.
private void setActionBarCheckboxChecked(MenuItem it, boolean checked)
{
if (it == null)
return;
it.setChecked(checked);
// Since it is shown as an action, and not in the sub-menu we have to manually set the icon too.
CheckBox cb = (CheckBox)it.getActionView().findViewById(R.id.action_item_checkbox);
if (cb != null)
cb.setChecked(checked);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
// Restore the check state e.g. if the device has been rotated.
final MenuItem logItem = menu.findItem(R.id.menu_action_logging);
setActionBarCheckboxChecked(logItem, mRetainedFragment.getLoggingEnabled());
CheckBox cb = (CheckBox)logItem.getActionView().findViewById(R.id.action_item_checkbox);
if (cb != null)
{
// Set the text to match the item.
cb.setText(logItem.getTitle());
// Add the onClickListener because the CheckBox doesn't automatically trigger onOptionsItemSelected.
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(logItem);
}
});
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_logging:
// Toggle the checkbox.
setActionBarCheckboxChecked(item, !item.isChecked());
// Do whatever you want to do when the checkbox is changed.
mRetainedFragment.setLoggingEnabled(item.isChecked());
return true;
default:
break;
}
return false;
}