Skip to content

Instantly share code, notes, and snippets.

@MRezaNasirloo
Last active May 24, 2021 06:37
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MRezaNasirloo/110310338c81ce1f90c8eb9a828b6c88 to your computer and use it in GitHub Desktop.
Save MRezaNasirloo/110310338c81ce1f90c8eb9a828b6c88 to your computer and use it in GitHub Desktop.
A Backward Compatible TextView drawableTint
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewCompatTint">
<attr name="drawableTint" format="reference|color"/>
<attr name="drawableTintMode" format="reference"/>
</declare-styleable>
</resources>
<!--TODO replace with your package-->
<com.github.pedramrn.TextViewCompatTint
android:id="@+id/textView_replies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="4dp"
android:drawableStart="@drawable/ic_comment_black_14dp"
app:drawableTint="@color/colorPrimary"
android:gravity="center"
tools:text="28"
/>
package com.github.pedramrn;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import com.github.pedramrn.slick.parent.R;
/**
* @author : Pedramrn@gmail.com
* Created on: 2017-04-27
*/
public class TextViewCompatTint extends AppCompatTextView {
public TextViewCompatTint(Context context) {
this(context, null);
}
public TextViewCompatTint(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public TextViewCompatTint(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextViewCompatTint, defStyleAttr, 0);
if (typedArray.hasValue(R.styleable.TextViewCompatTint_drawableTint)) {
int color = typedArray.getColor(R.styleable.TextViewCompatTint_drawableTint, 0);
Drawable[] drawables = getCompoundDrawablesRelative();
for (Drawable drawable : drawables) {
if (drawable == null) continue;
DrawableCompat.setTint(DrawableCompat.wrap(drawable).mutate(), color);
}
}
typedArray.recycle();
}
}
@gbzarelli
Copy link

for api level < 17 change: getCompoundDrawablesRelative() (Line 35) to TextViewCompat.getCompoundDrawablesRelative(this)

Copy link

ghost commented Mar 22, 2019

@MRezaNasirloo would you mind adding a license (MIT?) to this gist if it is not too much trouble? I'm guessing you created this gist for other people to use but without a license no one can use that code (see https://help.github.com/en/articles/licensing-a-repository).

However, without a license, the default copyright laws apply, meaning that you retain all rights to your source code and no one may reproduce, distribute, or create derivative works from your work. If you're creating an open source project, we strongly encourage you to include an open source license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment