Android MenuItem Toggle Button

Isaac Waller picture Isaac Waller · Feb 20, 2009 · Viewed 14.4k times · Source

In my Android application, I want a setting to be toggleable in it's menu like the Dialer app does for speaker and mute. You can see a picture below:

http://www.isaacwaller.com/images/acall.png

You see how the Speaker, Mute and Hold options are toggle buttons - you can tap them again and they will toggle the green color. They may do this in a custom way, but I suspect it is a option (I tried setting the Checkable attribute).

Answer

Quintin Robinson picture Quintin Robinson · Feb 21, 2009

You could do something like the snippet below as well, originally sourced from anddev.org

public boolean onPrepareOptionsMenu(final Menu menu) {       
      if(super.mMapView.isTraffic()) 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48); 
      else 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48); 

      return super.onPrepareOptionsMenu(menu); 
 }