Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.PorterDuff;
- import android.graphics.Typeface;
- import android.graphics.drawable.Drawable;
- import android.os.Build;
- import android.support.v4.graphics.drawable.DrawableCompat;
- import android.support.v7.content.res.AppCompatResources;
- import android.support.v7.widget.AppCompatTextView;
- import android.text.TextUtils;
- import android.util.AttributeSet;
- public class ButtonTextView extends AppCompatTextView {
- public ButtonTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(attrs, context);
- }
- public ButtonTextView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(attrs, context);
- }
- private void init(AttributeSet attrs, Context context) {
- if (attrs != null) {
- TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ButtonTextView, 0, 0);
- int colorFilter = a.getColor(R.styleable.ButtonTextView_colorFilter, 0xffffffff);
- Drawable drawableLeft = null;
- Drawable drawableRight = null;
- Drawable drawableBottom = null;
- Drawable drawableTop = null;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- drawableLeft = a.getDrawable(R.styleable.ButtonTextView_drawableLeftCompat);
- drawableRight = a.getDrawable(R.styleable.ButtonTextView_drawableRightCompat);
- drawableTop = a.getDrawable(R.styleable.ButtonTextView_drawableTopCompat);
- drawableBottom = a.getDrawable(R.styleable.ButtonTextView_drawableBottomCompat);
- } else {
- final int drawableLeftId = a.getResourceId(R.styleable.ButtonTextView_drawableLeftCompat, 0);
- final int drawableRightId = a.getResourceId(R.styleable.ButtonTextView_drawableRightCompat, 0);
- final int drawableTopId = a.getResourceId(R.styleable.ButtonTextView_drawableTopCompat, 0);
- final int drawableBottomId = a.getResourceId(R.styleable.ButtonTextView_drawableBottomCompat, 0);
- if (drawableLeftId != 0) {
- drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
- }
- if (drawableRightId != 0) {
- drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
- }
- if (drawableTopId != 0) {
- drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
- }
- if (drawableBottomId != 0) {
- drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
- }
- }
- if (drawableLeft != null) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- drawableLeft.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
- } else {
- DrawableCompat.setTint(drawableLeft, colorFilter);
- }
- }
- if (drawableRight != null) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- drawableRight.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
- } else {
- DrawableCompat.setTint(drawableRight, colorFilter);
- }
- }
- if (drawableTop != null) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- drawableTop.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
- } else {
- DrawableCompat.setTint(drawableTop, colorFilter);
- }
- }
- if (drawableBottom != null) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
- drawableBottom.mutate().setColorFilter(colorFilter, PorterDuff.Mode.SRC_IN);
- } else {
- DrawableCompat.setTint(drawableBottom, colorFilter);
- }
- }
- if (drawableLeft != null) {
- drawableLeft.setBounds(0, 0, drawableLeft.getIntrinsicWidth(), drawableLeft.getIntrinsicHeight());
- }
- if (drawableRight != null) {
- drawableRight.setBounds(0, 0, drawableRight.getIntrinsicWidth(), drawableRight.getIntrinsicHeight());
- }
- if (drawableTop != null) {
- drawableTop.setBounds(0, 0, drawableTop.getIntrinsicWidth(), drawableTop.getIntrinsicHeight());
- }
- if (drawableBottom != null) {
- drawableBottom.setBounds(0, 0, drawableBottom.getIntrinsicWidth(), drawableBottom.getIntrinsicHeight());
- }
- setCompoundDrawablesRelative(drawableLeft, drawableTop, drawableRight, drawableBottom);
- String fontName = a.getString(R.styleable.ButtonTextView_font_name);
- if (!TextUtils.isEmpty(fontName)) {
- Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
- "fonts/".concat(fontName));
- setTypeface(tf);
- }
- a.recycle();
- }
- }
- }
Add Comment
Please, Sign In to add comment