3

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?

Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31

6 Answers6

1

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>
Community
  • 1
  • 1
Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
1

I agree with your answer, but I had to do some digging to get it to work in my code, ultimately, I had to create an onClickListener for the button.

In my code, I wrote in the onCreateOptionsMenu(Menu menu) method:

menu.getItem(indexInMenu).getActionView().findViewById(R.id.button_logout).setOnClickListener(...);

This makes the menuItem responsive when it's replaced with a button in app:actionLayout

Nick
  • 81
  • 4
0

I have try your menu xml, and it worked on android 4.1.1, 5.0.0

but it not worked on 4.3

to fix this, please refer to How to get text on an ActionBar Icon?

it use custom layout for the item

Community
  • 1
  • 1
0

You could try:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >
    <!-- "Mark Favorite", should appear as action button if possible -->
    <!-- Settings, should always be in the overflow -->
    <item
        android:title="Settings"
        android:id="@+id/mainMenu"
        android:icon="@drawable/ic_3d_rotation_black_24dp"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/menu_logout1"
                android:icon="@drawable/ic_3d_rotation_black_24dp"
                android:title="logout1"/>
            <item
                android:id="@+id/menu_logout3"
                android:icon="@drawable/ic_3d_rotation_black_24dp"
                android:title="logout3"/>
        </menu>
    </item>
    <item
        android:id="@+id/menu_logout2"
        android:icon="@drawable/ic_3d_rotation_black_24dp"
        android:title="logout2"
        app:showAsAction="always"/>
</menu>

Note, that this example will also paint some icons, so if you don“t want the icons, just remove the icon attribute.

linker85
  • 1,601
  • 5
  • 26
  • 44
0

Remove icon from <item

android:icon="@drawable/ic_baseline_add_24"

Remove this line then it will show hope it worked for u

Vaibhav
  • 21
  • 3
0

When the text in your toolbar menu items is not showing, this is probably related to your style settings. For example, when you have organized your style in a way that app title in toolbar is shown in white color, your menu items may become white on white.

To solve this, you need to set two attributes to your toolbar:

  1. This is the general Style for the toolbar. Textcolor is white. Maybe you are using something like this when you are facing the "no text"-issue
<style name="ToolBarStyle" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorSecondary">@android:color/white</item>
</style>

  1. To make sure that text in menu is showing, not only set the app:theme of your toolbar but also the app:popuptheme. In this example popuptheme is set to a light theme because this leads to black text.
<androidx.appcompat.widget.Toolbar
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:background="@color/supplyon_blue"
            app:theme="@style/ToolBarStyle"
            app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
            android:id="@+id/toolbar" />

Play around with this setting and it will probably solve your problem.

Nick
  • 65
  • 8