5

I've created a menu with a single item.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/syncButton"
        android:title="Sync"
        android:icon="@drawable/ic_sub_menu"
        app:showAsAction="never"/>
</menu>

This is used on some of my activities in the toolbar, when clicked it drops down a menu, currently there is only one option but in the future it may be more.

Everything works well except the icon, it's a vector image of the traditional 3 dots colored white. Depending on what showAsAction" is set as it changes color.

Currently showAsAction is set to never so it displays a menu when clicked, this is what I want, but the icon changes to a dark grey. If i set this option to "always" then the icon changes to white but I lose the drop down menu.

How can I keep the drop down menu while keeping my icon white ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Demonic218
  • 893
  • 2
  • 15
  • 33
  • try this https://stackoverflow.com/questions/19882443/how-to-change-menuitem-icon-in-actionbar-programmatically – Ankita Jul 04 '18 at 07:38
  • basically you want to change the 3dots icon to 3whiteDots? – Aiko West Jul 04 '18 at 07:39
  • The toolbar displays the 3 dots icon colored grey by default to indicate where you click to drop the menu. So you added in your menu an item that has an icon with 3 dots? –  Jul 04 '18 at 07:42
  • So you're saying that the grey is the default when there is a drop down menu and is overriding my icon that I've set, I didn't know it did that. Thank you – Demonic218 Jul 04 '18 at 08:05

2 Answers2

3

Try this code

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    menuInflater.inflate(R.menu.menu_confirm, menu);
    MenuItem action_done = menu.findItem(R.id.action_done);
    action_done.setIcon(R.drawable.ic_filter);
    menuIconColor(action_done, Color.WHITE);
    super.onCreateOptionsMenu(menu, menuInflater);
}

public void menuIconColor(MenuItem menuItem, int color) {
    Drawable drawable = menuItem.getIcon();
    if (drawable != null) {
        drawable.mutate();
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
}
Hardik Vasani
  • 876
  • 1
  • 8
  • 14
3

If you just want to change the 3-Dots-Icon for the DropDownMenu, you can change this in your Styles.xml:

In your Theme Style define

<item name="android:actionOverflowButtonStyle">@style/MyTheme.OverFlow</item>

and then define the Overflow with your Icon you want to show instead the 3DotsIcon (in your case the Icon with 3 white dots)

  <style name="MyTheme.OverFlow">
    <item name="android:src">@drawable/yourNewIcon</item>
  </style>
Aiko West
  • 791
  • 1
  • 10
  • 30