the_prole
the_prole

Reputation: 8985

How to change AppCompatButton menu item text

I'm using the following layout for my menu

<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">

    <item android:id="@+id/button"
        android:title="off"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.support.v7.widget.AppCompatButton"
        tools:text="OFF"
        android:textColor="@android:color/white"/>
</menu>

There doesn't seem to be an obvious way to set text to the button. The following two lines I added did not result in any visible text being added to the button.

tools:text="OFF"
android:textColor="@android:color/white"

edit:

To be clear, this a menu item, but the action view class is AppCompatButton and I want to set text to it.

Upvotes: 2

Views: 1149

Answers (1)

azizbekian
azizbekian

Reputation: 62209

You can get the action view from MenuItem, and perform necessary changes there:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.findItem(R.id.button);
    AppCompatButton button = (AppCompatButton) menuItem.getActionView();
    button.setText("My Button");
    return super.onPrepareOptionsMenu(menu);
}

enter image description here

Upvotes: 1

Related Questions