0

I just want to set the colour of the vector asset. I have searched for it but doesn't seem to be work for me. and I don't want to change it from XML, I want to change it in my MainActivity.java, or I say programmatically.is there any method for this?

Here is my Code

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.add("Image");
    menuItem.setIcon(R.drawable.ic_camera_alt_black_24dp);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    //something like this
    //menuItem.setIconColor(Color.WHITE);
    return true;
}

I am searching something for formatting the icon colour.

I mentioned it that I don't want to do this using XML.And as well android:tint does not work for me

meditat
  • 1,197
  • 14
  • 33

4 Answers4

2
ivImageView.setColorFilter(getResources().getColor(R.color.colorPrimary));
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
2

try below code

Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_camera_alt_black_24dp, null);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, Color.WHITE);
menuItem.setIcon(drawable);
Munir
  • 2,548
  • 1
  • 11
  • 20
1

Changing colour for a Vector Drawable should be for an ImageView or Drawable:

yourImageView.setColorFilter(ContextCompat.getColor(context, R.color.your_color_white), android.graphics.PorterDuff.Mode.SRC_IN);

This will work for Vector images ImageView or Drawable as ImageView

Xenolion
  • 12,035
  • 7
  • 33
  • 48
1
menuItem.getIcon().setColorFilter(Color.WHITE , PorterDuff.Mode.SRC_ATOP);
meditat
  • 1,197
  • 14
  • 33