Search code examples
androidtextcolorsandroid-actionbar

How to change Action Bar text based button color?


I had already had some issues with the overflow button being black and wanting it white but after some trial and error I got it working. But I just can't get this text button to be white. It's just a string based item in the main_toolbar.xml

<item
        android:id="@+id/more"
        android:title="@string/more"
        app:showAsAction="always"/>

Link to the image

After playing around for several hours in styles.xml and looking for solutions in here, I gave up and just decided to ask for my specific problem. Thanks in advance!


Solution

  • You can change the color of the MenuItem text easily by using SpannableString instead of String

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.your_menu, menu);
    
    int positionOfMenuItem = 0; // or whatever...
    MenuItem item = menu.getItem(positionOfMenuItem);
    SpannableString s = new SpannableString("My red MenuItem");
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
    item.setTitle(s);
    }