Last active
May 24, 2021 06:37
-
-
Save MRezaNasirloo/110310338c81ce1f90c8eb9a828b6c88 to your computer and use it in GitHub Desktop.
A Backward Compatible TextView drawableTint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--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" | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : [email protected] | |
* 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(); | |
} | |
} |
@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
for api level < 17 change: getCompoundDrawablesRelative() (Line 35) to TextViewCompat.getCompoundDrawablesRelative(this)