Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display both icon and title of action inside ActionBar?

I need to display both icon and title of action inside ActionBar.

I've tried "withText" option, but it has no effect.

enter image description here

like image 951
Alexey Zakharov Avatar asked Aug 17 '12 06:08

Alexey Zakharov


People also ask

How do you add action items to the action bar in Android?

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.

What is the difference between ActionBar and toolbar?

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 toolbar and action bar in Android?

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.


2 Answers

'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" /> 
like image 60
Yibo Long Avatar answered Sep 20 '22 03:09

Yibo Long


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.

like image 38
Benito Bertoli Avatar answered Sep 20 '22 03:09

Benito Bertoli