Skip to content

Instantly share code, notes, and snippets.

@allenatwork
Forked from tylerchesley/TintableImageView.java
Last active July 1, 2016 04:14
Show Gist options
  • Save allenatwork/b1f3d361dfe9d92a9e9d3270d89e121d to your computer and use it in GitHub Desktop.
Save allenatwork/b1f3d361dfe9d92a9e9d3270d89e121d to your computer and use it in GitHub Desktop.
Backwards compatible TintableImageView
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="TintableImageView">
<attr name="tint" format="reference|color" />
</declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/pressed_color"/>
<item android:color="#00000000"/>
</selector>
package biz.nadia.rola.widget.tablayout;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import biz.nadia.rola.R;
import biz.nadia.rola.layout.RolaTextView;
/**
* Created by Allen on 01-Jul-16.
*/
public class TintableRolaTextView extends RolaTextView {
private ColorStateList tint;
public TintableRolaTextView(Context context) {
super(context);
}
public TintableRolaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public TintableRolaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintableRolaTextView, defStyle, 0);
tint = a.getColorStateList(R.styleable.TintableRolaTextView_text_tint);
a.recycle();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null && tint.isStateful()) {
updateTintColor();
}
}
private void updateTintColor() {
int color = tint.getColorForState(getDrawableState(), 0);
setTextColor(color);
}
}
package com.example.widgets;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.example.R;
public class TintableImageView extends ImageView {
private ColorStateList tint;
public TintableImageView(Context context) {
super(context);
}
public TintableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public TintableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle, 0);
tint = a.getColorStateList(R.styleable.TintableImageView_tint);
a.recycle();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null && tint.isStateful())
updateTintColor();
}
public void setColorFilter(ColorStateList tint) {
this.tint = tint;
super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}
private void updateTintColor() {
int color = tint.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment