Set textview default color on Android

2

I have a custom TextView, it currently directs the color of the text to white, however, I wanted this to be the default color, that is, to be chosen white if no color is set with textColor in xml, how to do this ?

xml: (I tried to set textColor directly, however, it ends up setting white, because of Java)

<com.app.customview.DefaultTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/especifico_label"
            android:textSize="28sp"
            android:layout_marginTop="9dp"
            android:layout_gravity="center"/>

Java:

public class DefaultTextView extends android.support.v7.widget.AppCompatTextView {

    private boolean isLight = false;

    public DefaultTextView(Context context) {
        super(context);
        setFont();
    }

    public DefaultTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAttr(context, attrs);
        setFont();
    }

    public DefaultTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setAttr(context, attrs);
        setFont();
    }

    private void setAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Font, 0, 0);
        isLight = typedArray.getBoolean(R.styleable.Font_isLight, false);
        typedArray.recycle();
    }

    private void setFont() {
        if(!isInEditMode()) {
            String tf;

            if(getTypeface()!=null) {
                switch (getTypeface().getStyle()) {
                    case Typeface.ITALIC:
                        tf = "fonts/segoeuii.ttf";
                        break;
                    default:
                        tf = "fonts/segoeui.ttf";
                        break;
                }

            } else {
                tf = "fonts/segoeui.ttf";
            }

            if(isLight) {
                tf = "fonts/segoeuil.ttf";
            }

            setTypeface(Typeface.createFromAsset(getContext().getAssets(), tf));
            setTextColor(Color.WHITE);
        }
    }
}
    
asked by anonymous 20.08.2017 / 04:10

1 answer

2

Just get the value that was (or not) set in textColor, if it is null (no color was defined in XML), then seven is white.

color = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textColor");

if(color==null) {
   setTextColor(Color.WHITE);
}
    
21.08.2017 / 06:49