How to control ActionBar split programmatically?

paulovic picture paulovic · Nov 25, 2011 · Viewed 12.1k times · Source

The android ActionBar may split into a top and bottom bars if activity's parameter "uiOptions" is set to "splitActionBarWhenNarrow", note this parameter is only valid in ICS.

Honeycomb has introduced a new approach to multi-select list items using action bar. When a item is under press & hold the list becomes into a multi-selection mode and the actionbar bar may be used to accomplish some actions. The actionbar setup is inherited from the list activity, i.e., if the activity has a split action bar the multi-selection will have too, and if the activity has only the top bar, so, the multi-selection will be compliant with that.

The question is, is it possible to have only a top action bar in the activity and when the list turns into multi-selection mode programmatically split the actionbar?

Thanks!

Answer

adamp picture adamp · Nov 26, 2011

No, you cannot switch between split and non-split action bars on the fly.

The setter counterpart to android:uiOptions is on Window, not Activity. Window#setUiOptions is the method and the flag to use is ActivityInfo#UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW.

However this isn't going to do what you want. Split action bar when narrow must be specified as the window is first configured before the window decor is initialized. In other words, once the window has been displayed (or even once you've called setContentView) it's too late to change it.

This was a conscious decision by the Android UX team. Action modes (including selection modes) are meant to mirror the configuration of the action bar on the current activity. This gives the user a single place to look for currently valid actions within the same activity.