Skip to content

Instantly share code, notes, and snippets.

@robsn
Created September 19, 2019 11:12
Show Gist options
  • Save robsn/bdd6221074ba393a84e4b1d65248f0e4 to your computer and use it in GitHub Desktop.
Save robsn/bdd6221074ba393a84e4b1d65248f0e4 to your computer and use it in GitHub Desktop.
public final class DrawableCompat
{
private DrawableCompat()
{
}
/**
* Tints the given drawable with the given color.
*
* <br>For Android versions < Lollipop {@link Drawable#setColorFilter(int, PorterDuff.Mode)} will be used.
* <br>For Android versions >= Lollipop the drawables will be colored via {@link Drawable#setTint(int)}
*
* @param context The context.
* @param drawable The drawable to be tinted.
* @param colorResId The color resource to be applied.
*/
@SuppressWarnings( "squid:CallToDeprecatedMethod" )
public static void tintDrawableWithColorResId( final Context context, final Drawable drawable, @ColorRes final int colorResId )
{
final int tintColor = ContextCompat.getColor( context, colorResId );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
drawable.setTint( tintColor );
}
else
{
drawable.setColorFilter( tintColor, SRC_ATOP );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment