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 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

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
  • Old question, but just a note to anyone looking - when using menu.findItem() use the item id rather than a index number. i.e. menu.findItem(R.id.menu_item) – enyciaa Feb 23 '16 at 15:31

2 Answers2

4

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
  • 50,022
  • 30
  • 123
  • 131
  • 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 Aug 06 '10 at 14:29
  • 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 Aug 06 '10 at 15:01
  • Its standard from Android 1.6, Vanilla as in its pure Android 1.6, not a custom ROM – Donal Rafferty Aug 06 '10 at 15:50
  • 1
    Did you look at the link I added in my edit? I think the code could be what you need. – Sephy Aug 06 '10 at 16:00
  • 1
    This isn't an API function. It's manually changing the drawable for the options button. Options buttons can't be checkboxes – Falmarri Aug 06 '10 at 17:46
1

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.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49