How to display both icon and title of action inside ActionBar?
'always|withText' will work if there is sufficient room, otherwise it will only place icon. You can test it on your phone with rotation.
<item android:id="@id/menu_item"android:title="text"android:icon="@drawable/drawable_resource_name"android:showAsAction="always|withText" />
You can create actions with text in 2 ways:
1- From XML:
<item android:id="@id/resource_name" android:title="text" android:icon="@drawable/drawable_resource_name" android:showAsAction="withText" />
When inflating the menu, you should call getSupportMenuInflater()
since you are using ActionBarSherlock
.
2- Programmatically:
@Overridepublic boolean onCreateOptionsMenu(Menu menu) { MenuItem item = menu.add(Menu.NONE, ID, POSITION, TEXT); item.setIcon(R.drawable.drawable_resource_name); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT); return true;}
Make sure you import com.actionbarsherlock.view.Menu
and com.actionbarsherlock.view.MenuItem
.
What worked for me was using 'always|withText'. If you have many menus, consider using 'ifRoom' instead of 'always'.
<item android:id="@id/resource_name"android:title="text"android:icon="@drawable/drawable_resource_name"android:showAsAction="always|withText" />