public static TintedDrawable wrap(Drawable drawable) { if (drawable instanceof TintedDrawable) { return (TintedDrawable) drawable; } return new TintedDrawable(drawable.mutate()); }
private void init() { // Unfortunately, drawableStart and drawableEnd set through XML does not call the setter, // so manually getting it and wrapping it in the compat drawable. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Drawable[] drawables = getCompoundDrawablesRelative(); for (int i = 0; i < drawables.length; i++) { if (drawables[i] != null) { drawables[i] = TintedDrawable.wrap(drawables[i]); } } setCompoundDrawablesRelativeWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]); } }
@Override public boolean setState(int[] stateSet) { boolean needsInvalidate = super.setState(stateSet); boolean needsInvalidateForState = updateState(); return needsInvalidate || needsInvalidateForState; }
@Override public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, Drawable bottom) { if (start != null) start = TintedDrawable.wrap(start); if (top != null) top = TintedDrawable.wrap(top); if (end != null) end = TintedDrawable.wrap(end); if (bottom != null) bottom = TintedDrawable.wrap(bottom); super.setCompoundDrawablesRelative(start, top, end, bottom); tintDrawables(); }
private boolean updateState() { if (mTintList != null) { final int color = mTintList.getColorForState(getState(), 0); setColorFilter(color, PorterDuff.Mode.SRC_IN); return true; // Needs invalidate } return false; } }
private void tintDrawables() { final ColorStateList textColors = getTextColors(); if (textColors != null) { for (Drawable drawable : getAllCompoundDrawables()) { if (drawable instanceof TintedDrawable) { ((TintedDrawable) drawable).setTintListCompat(textColors); } } invalidate(); } }
@Override public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) { if (left != null) left = TintedDrawable.wrap(left); if (top != null) top = TintedDrawable.wrap(top); if (right != null) right = TintedDrawable.wrap(right); if (bottom != null) bottom = TintedDrawable.wrap(bottom); super.setCompoundDrawables(left, top, right, bottom); tintDrawables(); }
public void setTintListCompat(ColorStateList colors) { mTintList = colors; if (updateState()) { invalidateSelf(); } }