views:

142

answers:

1

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


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

+2  A: 

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.

Sephy
I have added that method, I have edited my question to show you my code, but the outcome is the same, I cant get it to produce the check light that turns on and off (The green one)
Donal Rafferty
Actually, the image you are displaying looks like a custom view... Is it something you found on the web? Cause maybe this is it not possible directly from the basic API and you'd have to build your own menu. I don't remember ever seeing a menu in android with the icon below the text.
Sephy
Its standard from Android 1.6, Vanilla as in its pure Android 1.6, not a custom ROM
Donal Rafferty
Did you look at the link I added in my edit? I think the code could be what you need.
Sephy
This isn't an API function. It's manually changing the drawable for the options button. Options buttons can't be checkboxes
Falmarri