2

I have created a popup menu and inside the popup.xml, I have two icons. I trying to change the color of icon inside the popup.xml but the color always remains white (original color).

Is there a way to change the drawable color in menu ?

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/opt1"
        android:icon="@drawable/change_pic"
        android:color="@color/green"
        android:title="Change Picture" />
    <item
        android:id="@+id/opt2"
        android:icon="@drawable/change_pin"
        android:title="Change Password" />
</menu>

ScreenShot

enter image description here

Code

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_task, menu); // for the two icons in action bar
        return true;
    }

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

                case R.id.menu:
                    View menuItemView = findViewById(R.id.menu);
                    MenuBuilder menuBuilder =new MenuBuilder(this);
                    MenuInflater inflater = new MenuInflater(this);
                    inflater.inflate(R.menu.popup, menuBuilder);
                    MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
                    optionsMenu.setForceShowIcon(true);
                    optionsMenu.show();

                default:
                    return super.onOptionsItemSelected(item);
            }
        }

As you can see, the image is actually white color.

John Joe
  • 12,412
  • 16
  • 70
  • 135

2 Answers2

2

Please try this

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu:
                View menuItemView = findViewById(R.id.menu);
                MenuBuilder menuBuilder =new MenuBuilder(this);
                MenuInflater inflater = new MenuInflater(this);
                inflater.inflate(R.menu.popup, menuBuilder);
 Drawable yourdrawable1 = menuBuilder.getItem(0).getIcon(); // change 0 with 1,2 ... 
            Drawable yourdrawable2 = menuBuilder.getItem(1).getIcon();
            yourdrawable1.mutate();
            yourdrawable2.mutate();
             yourdrawable1.setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_IN);
yourdrawable2.setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_IN);
                MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, menuItemView);
                optionsMenu.setForceShowIcon(true);
                optionsMenu.show();

            default:
                return super.onOptionsItemSelected(item);
        }
    }
Athul
  • 801
  • 7
  • 16
1

change_pic is image file or vector asset file ??? If it is vector asset then you can change the color from .xml file and if you are using image file then i suggest you to use vector asset file.

appy
  • 106
  • 1
  • 5
  • I download from https://material.io/icons/#ic_power_settings_new. Is it consider vector asset file ? – John Joe Dec 14 '16 at 06:24
  • save it in .svg format and then you can convert it in vector asset.for converting .svg to vector asset go to drawable>>new>>vector asset>>select local file(svg,psd) and then select your .svg file path. – appy Dec 14 '16 at 11:57
  • It will generate one .xml file.In .xml file you can change the color from android:fillColor="#000000". put your color code. – appy Dec 14 '16 at 11:59