9
 <ImageView
     android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@drawable/star_icon"
      android:id="+@starid"/>

I need to change tint color of icon programmatically help me

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
sukesh
  • 95
  • 1
  • 1
  • 6

5 Answers5

31

Thanks for Hardik, orginal answer

You can change the tint, quite easily in code via:

imageView.setColorFilter(Color.argb(255, 255, 255, 255));

If you want color tint then:

imageView.setColorFilter(ContextCompat.getColor(context,
R.color.COLOR_YOUR_COLOR));

with mode:

imageView.setColorFilter(ContextCompat.getColor(context,
R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);

in my case:

imgBottomDivider.setColorFilter(ContextCompat.getColor(getContext(),
            mPreferences.isNightMode() ? R.color.colorWhite : R.color.colorBlack));

in night mode in day mode

Abror Esonaliev
  • 11,797
  • 2
  • 23
  • 20
5

use

imageView.setColorFilter(int color, PorterDuff.Mode mode) 

or

imageView.setColorFilter(int color)

Reference: https://developer.android.com/reference/android/widget/ImageView.html#setColorFilter(int)

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
4

Try this,

imageViewDone.setColorFilter(ContextCompat.getColor(context, R.color.yourcolor), android.graphics.PorterDuff.Mode.MULTIPLY);
Lokesh Desai
  • 2,607
  • 16
  • 28
1

You can use Color Filter

  imageView.setColorFilter(ContextCompat.getColor(mContext, R.color.CHOICE_COLOUR), android.graphics.PorterDuff.Mode.MULTIPLY);
Pratik Sinha
  • 234
  • 1
  • 14
1

If you want to use default Duff mode for the imagevview you can prefer to

imageView.setColorFilter(int color)

but if you want to change the colour duff like light to multiple or dark then you should use:

imageView.setColorFilter(int color, PorterDuff.Mode mode) 
Android Geek
  • 8,956
  • 2
  • 21
  • 35