Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.support.v4.content.ContextCompat; public class Main { /** * Set the drawable to a specific color and return it * @param drawable The drawable to change * @param colorToSet The color to set it to * @return Drawable * @throws NullPointerException, if it fails, throws a null pointer */ public static Drawable colorDrawable(Drawable drawable, int colorToSet) { try { drawable.mutate().setColorFilter(colorToSet, PorterDuff.Mode.MULTIPLY); return drawable; } catch (Exception e) { e.printStackTrace(); throw new NullPointerException(); } } /** * Set the drawable to a specific color and return it * @param drawableId the int ID of the drawable to change * @param colorToSet The color to set it to * @return Drawable * @throws NullPointerException, if it fails, throws a null pointer */ public static Drawable colorDrawable(int drawableId, int colorToSet, Context context) { try { Drawable drawable = ContextCompat.getDrawable(context, drawableId); drawable.mutate().setColorFilter(colorToSet, PorterDuff.Mode.MULTIPLY); return drawable; } catch (Exception e) { e.printStackTrace(); throw new NullPointerException(); } } }