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;
}
Related
is possible overade hardware menu button? When i tap on hardware menu button, appear new layout page, instead of view of submenu? My basic app do not have any setting so only what i want is credits page. I try lot of options. I mean something like this:
#override
public boolean onCreateOptionsMenu() {
setContentView(R.layout.credits); }
Every help would be great.
From option menu You must return true for the menu to be displayed; if you return false it will not be shown.
So you can use
#override
public boolean onCreateOptionsMenu() {
setContentView(R.layout.credits);
return false;
}
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"/>
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);
}
Can anyone point in the direction of any tutorials that show how to create an options menu with clicakble checks like in the picture below:
alt text http://img291.imageshack.us/img291/1221/deviceit.png
I have tried as follows:
/** Menu creation and setup **/
/* Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, "Speaker").setCheckable(true);
menu.add(0, 2, 0, "Mute").setCheckable(true);
return result;
}
/* Handles item selections */
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
if(audioManager.isSpeakerphoneOn()==false){
audioManager.setSpeakerphoneOn(true);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_SPEAKER, AudioManager.ROUTE_ALL);
}else{
audioManager.setSpeakerphoneOn(false);
audioManager.setRouting(AudioManager.MODE_IN_CALL,
AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
}
return true;
case 2:
if(audioManager.isMicrophoneMute())
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
return true;
}
return false;
}
But this doesn't work it only gives me text on the buttons on the options menu
EDIT: I have added the following onPrepareOptionsMenu method:
public boolean onPrepareOptionsMenu(Menu menu){
boolean result = super.onPrepareOptionsMenu(menu);
if(audioManager.isSpeakerphoneOn())
menu.findItem(1).setChecked(true);
else
menu.findItem(1).setChecked(false);
if(audioManager.isMicrophoneMute())
menu.findItem(2).setChecked(true);
else
menu.findItem(2).setChecked(false);
return result;
}
However I get the same outcome just text and no check light as in the picture above
If you want to change dynamically the state of your Option Menu, you need to use onPrepareMenu(). In this method, you can do dynamic checks and update anything you want.
Good luck!!
documentation
After some digging, this look like a custom view. I think your picture comes from this code.
This is an old question, but I had the same problem and searched a lot to find such an optionsMenu shown above. I found a tutorial on http://www.codeproject.com and modified it a little bit. Maybe it is not a profi-programmers-code, but it works for me. See my modifications at my (poor arranged) web page on google sites (I got an little tutorial too on this site):
https://sites.google.com/site/opiatefuchs/android-code-examples
This code is original from wjfrancis on the Code Project web page (many props):
http://www.codeproject.com/Articles/173121/Android-Menus-My-Way
I just modified it and would be glad if somebody got any ideas to improve this code. But for now, it works.