Search code examples
androidandroid-actionbar

ActionBar menuItem not showing text


I have a menu layout with only one item in it.

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

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/toolbar_right_button"
        android:icon="@drawable/ic_toolbar_locked"
        android:orderInCategory="1"
        android:title="Logout"
        app:showAsAction="always|withText"/>
</menu>

As you can see I've included the withText attribute but I still only get the icon.

How can I get both the icon and text to show?


Solution

  • I'm not sure why withText wouldn't work for me so I just created an actionLayout to use instead.

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/toolbar_right_button"
            android:icon="@drawable/ic_toolbar_locked"
            android:orderInCategory="1"
            android:title="Logout"
            app:actionLayout="@layout/menu_item_logout"
            app:showAsAction="ifRoom|withText"/>
    </menu>
    

    and the actionLayout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/logout_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_toolbar_locked"
            android:gravity="center_vertical"
            android:text="Logout"
            android:textColor="@color/PrimaryColour"/>
    </RelativeLayout>