Action Bar Checkable Icons?

Go To


I was wondering if it were at all possible to have checkable icons in an action bar in ICS? Not with a check, but with a android:stateChecked property to play around with. I want it so when the user clicks on the icon, they enter a certain mode (and the background of the icon would be a bright color to let them know they are still in that mode), and when they click it again they are taken out of that mode.

I've played around with some selector xmls but nothing has worked...Any ideas?

EDIT: I just learned that Checkable items cannot appear in main menus, they can only be in submenus. However, is there a way to still get the behavior of a checkable? It's easy enough to have a boolean like isInModeA, but is there a way to programmatically change the background/icon of an Action bar item?

2012-04-03 21:51
by roboguy12


There is a way to programmatically change the background/icon.

You can use ActionBar.setBackgroundDrawable, for instance, or setCustomView, or setIcon (found on the same page).

You may consider using a dropdown-like ActionProvider to provide e.g. a radio-button-like "on/off" rather than a checkbox.

2012-04-03 22:00
by Jon O
Ah! I didn't think to use a dropdown box for this! Thank you - roboguy12 2012-04-03 22:14


One approach you could try is to keep the state saved in some variable and just display different items based on this state. Obviously, some of these items can change the state. You might look at doing this by implementing a custom action provider.

2012-04-03 22:00
by kabuko