77

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

Rahmathullah M
  • 2,676
  • 2
  • 27
  • 44
Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
  • 4
    did this problem get solved finally? if so, what is required to be done? – Ajay Nov 22 '12 at 11:21
  • 1
    Looks like the answer is here: http://stackoverflow.com/questions/9282122/android-4-0-text-on-the-action-bar-never-shows In short, this is by design and you have to provide a custom layout to work around it. – N8allan Aug 26 '14 at 16:43

10 Answers10

117

'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" />
Yibo Long
  • 1,287
  • 1
  • 8
  • 6
22

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.

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • `MenuItem item = menu.add(Menu.NONE, ID, POSITION, TEXT);` Here's ID is the layout id (e.g. R.layout.myMenuItem), POSITION is the position of the items shown in action bar (e.g. 1,2,3 rtl), TEXT is the title you want to set to the actionbar item. – Reaz Murshed Nov 05 '15 at 05:24
17

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" />
ebernie
  • 820
  • 1
  • 8
  • 13
15

The solution I find is to use custom action Layout: Here is XML for menu.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:Eventapp="http://schemas.android.com/apk/res-auto">
<!-- This is a comment. -->
    <item
        android:id="@+id/action_create"
        android:actionLayout="@layout/action_view_details_layout"
        android:orderInCategory="50"
        android:showAsAction = "always"/>
</menu>

The Layout is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:gravity="center"
    android:text="@string/create"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:gravity="center"
    android:src="@drawable/ic_action_v"/>

</LinearLayout>

this will show the icon and the text together.

To get the clickitem the the fragment or activity:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    //super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_details_fragment, menu);
    View view = menu.findItem(R.id.action_create).getActionView();
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}
Gilad Zelniker
  • 247
  • 3
  • 7
  • 1
    in case anyone get this error "Error:(4) No resource identifier found for attribute 'actionlayout' in package 'android'" this is solve by replacing "android:actionLayout" with "app:actionLayout" – cutiko Nov 09 '15 at 16:29
  • well the ripple effect gone – Charles Galvez Feb 04 '16 at 06:57
  • Somehow this doesnt show for me - it is just ignoring the layout and displaying nothing or title if I provide it (btw ommiting title gives warning but it compiles) – Srneczek Jun 21 '16 at 18:41
  • @CharlesGalvez you can place `android:background="?selectableItemBackground"` for `LinearLayout` along with `android:clickable="true"` and `android:focusable="true"` – Variag Jun 22 '18 at 10:59
  • Probably this works, I am not sure, because in my case nothing was shown in menu, but a button pressed. I think it drew white text on white Toolbar. So to override textcolor see https://stackoverflow.com/questions/43276345/android-toolbar-menu-text-color. – CoolMind Nov 23 '18 at 12:21
1

If any1 in 2017 is wondering how to do this programmatically, there is a way that i don't see in the answers

.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
Alen Zarkovic
  • 224
  • 2
  • 11
1

You can add button in toolbar

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="title">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="16dp"
            android:background="@color/transparent"
            android:drawableRight="@drawable/ic_your_icon"
            android:drawableTint="@drawable/btn_selector"
            android:text="@string/sort_by_credit"
            android:textColor="@drawable/btn_selector"
            />

    </android.support.v7.widget.Toolbar>

create file btn_selector.xml in drawable

<?xml version="1.0" encoding="utf-8" ?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:state_selected="true"
    android:color="@color/white"
    />
<item
    android:color="@color/white_30_opacity"
    />

java:

private boolean isSelect = false;

OnClickListener for button:

private void myClick() {
    if (!isSelect) {
       //**your code**//
        isSelect = true;
    } else {//**your code**//
        isSelect = false;
    }
    sort.setSelected(isSelect);
}
  • Adding the button to the toolbar indeed works, although Android Studio says "Element Button not allowed here ...". (Just tried that part. Did not implement the full solution. So I don't know, whether all the stuff works.) – user2808624 Jan 07 '18 at 11:11
0

Some of you guys have great answers, but I found some additional thing. If you want create a MenuItem with some SubMenu programmatically:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        SubMenu subMenu = menu.addSubMenu(0, Menu.NONE, 0, "Menu title");
        subMenu.getItem().setIcon(R.drawable.ic_action_child);
        subMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        subMenu.add(0, Menu.NONE, 0, "Subitem 1");
        subMenu.add(0, Menu.NONE, 1, "Subitem 2");
        subMenu.add(0, Menu.NONE, 2, "Subitem 3");

        return true;
    }
Phong Nguyen
  • 6,897
  • 2
  • 26
  • 36
0

Try adding a TextView to the menubar first and using setCompoundDrawables() to place the image on whichever side you want. Bond click activity to the textview in the end.

MenuItem item = menu.add(Menu.NONE, R.id.menu_item_save, 10, R.string.save);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
TextView textBtn = getTextButton(btn_title, btn_image);
item.setActionView(textBtn);
textBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
           // your selector here   }
    });

You can literally customize everything here:

public TextView getTextButton (String btn_title, Drawable btn_image) {
    TextView textBtn = new TextView(this);
    textBtn.setText(btn_title);
    textBtn.setTextColor(Color.WHITE);
    textBtn.setTextSize(18);
    textBtn.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
    textBtn.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    Drawable img = btn_image;
    img.setBounds(0, 0, 30, 30);
    textBtn.setCompoundDrawables(null, null, img, null); 
    // left,top,right,bottom. In this case icon is right to the text

    return textBtn;
}
-1

Using app:showAsAction="always|withText". I am using Android 4.1.1, but it should applicable anyway. Mine look like below

<item
        android:id="@+id/action_sent_current_data"
        android:icon="@drawable/ic_upload_cloud"
        android:orderInCategory="100"
        android:title="@string/action_sent_current_data"
        app:showAsAction="always|withText"/>
-3

Follow these steps:

  1. Add the Action Bar instance in the Java Code final ActionBar actionBar = getActionBar();
  2. Enable the Home Display Option actionBar.setDisplayShowHomeEnabled(false);
  3. Add the following code in the respective activity's manifest file android:logo=@drawable/logo and android:label="@string/actionbar_text"

I think this will help you

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
rao
  • 1
  • 1