The fundamental purpose of an action bar, besides navigation, is to present the user with a set of actions that can be performed.
By simply using the action bar, all the action items are added to the overflow:
<menu ... > <item android:id="@+id/action_refresh" android:icon="@drawable/ic_action_refresh" android:title="@string/action_refresh"/> </menu>
However, we can customize what items are displayed, and how they are displayed:
ActionBar
property, as well as more complex items, all that is needed is an attribute in the XML, showAsAction
:<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:showAsAction="ifRoom"/> </menu>
actionViewClass
attribute:<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:actionViewClass="android.support.v7.widget.SearchView"/> </menu>
actionLayout
attribute:<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:actionLayout="@layout/action_rating"/> </menu>
<menu ... xmlns:app="http://schemas.android.com/apk/res-auto"> <item ... app:showAsAction="ifRoom|collapseActionView"/> </menu>
Action item buttons are just traditional options menu items but are optionally always visible on the action bar.
The underlying logic to handle item selections is the same as that for the traditional options menu. No change is required to existing code inside the OnOptionsItemSelected()
method.
The value of the showAsAction
attribute can be ifRoom
, never
, or always
. This value can optionally be combined, using a pipe, with withText
and/or collapseActionView
.