I need to display both icon
and title
of action inside ActionBar
.
I've tried "withText
" option, but it has no effect.
All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
An Action bar is traditionally a part of an Activity opaque window decor controlled by the framework but a Toolbar may be placed at any level of nesting within a view hierarchy. The toolbar provides more feature than ActionBar . A Toolbar may contain a combination of elements from start to end.
What is the difference between the toolbar and the action bar? The most obvious difference between the two is the updated visual design of the toolbar. The toolbar no longer includes an icon on the left side and decreases some of the spacing between the action items on the right side.
'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:
@Override public 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With