Last active
August 30, 2018 09:59
-
-
Save mannodermaus/2f0c4089fcd20ef1ccc550600b56f99f to your computer and use it in GitHub Desktop.
Custom TextView implementation to allow VectorDrawableCompat to work with compound Drawables
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="CompatTextView"> | |
<attr name="drawableStart" format="reference"/> | |
<attr name="drawableTop" format="reference"/> | |
<attr name="drawableEnd" format="reference"/> | |
<attr name="drawableBottom" 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
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.drawable.Drawable; | |
import android.os.Build; | |
import android.support.annotation.Nullable; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v7.widget.AppCompatDrawableManager; | |
import android.support.v7.widget.AppCompatTextView; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class CompatTextView extends AppCompatTextView { | |
public CompatTextView(Context context) { | |
super(context); | |
init(null); | |
} | |
public CompatTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(attrs); | |
} | |
public CompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(attrs); | |
} | |
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
public CompatTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
super(context, attrs, defStyleAttr, defStyleRes); | |
init(attrs); | |
} | |
private void init(@Nullable AttributeSet attrs) { | |
if (attrs != null) { | |
Context context = getContext(); | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CompatTextView); | |
// Obtain DrawableManager used to pull Drawables safely, and check if we're in RTL | |
AppCompatDrawableManager dm = AppCompatDrawableManager.get(); | |
boolean rtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL; | |
// Grab the compat drawable resources from the XML | |
int startDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableStart, 0); | |
int topDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableTop, 0); | |
int endDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableEnd, 0); | |
int bottomDrawableRes = a.getResourceId(R.styleable.CompatTextView_drawableBottom, 0); | |
// Load the used drawables, falling back to whatever may be set in an "android:" namespace attribute | |
Drawable[] currentDrawables = getCompoundDrawables(); | |
Drawable left = startDrawableRes != 0 ? dm.getDrawable(context, startDrawableRes) : currentDrawables[0]; | |
Drawable right = endDrawableRes != 0 ? dm.getDrawable(context, endDrawableRes) : currentDrawables[1]; | |
Drawable top = topDrawableRes != 0 ? dm.getDrawable(context, topDrawableRes) : currentDrawables[2]; | |
Drawable bottom = bottomDrawableRes != 0 ? dm.getDrawable(context, bottomDrawableRes) : currentDrawables[3]; | |
// Account for RTL and apply the compound Drawables | |
Drawable start = rtl ? right : left; | |
Drawable end = rtl ? left : right; | |
setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom); | |
a.recycle(); | |
} | |
} | |
} |
Hello, method getCompoundDrawables() return drawables in order "start, top, end, and bottom". Why you use currentDrawables[1] for right (end), in 53 line ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code won't build at this line
My support version is '24.2.0'