Set textview default color on Android

Asked

Viewed 895 times

2

I have a custom Textview, currently it arrow the color of the text to white, however, I wanted this to be the default color, IE, that was chosen white if not set a color with textColor in xml, how to do this?

xml: (I tried to set textColor directly, but 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);
        }
    }
}
  • If I’m not mistaken, by inheriting from Appcompattextview, it will pick up from the theme automatically.

  • Have you tried removing setTextColor(Color.WHITE); ?! Hehe

  • @white, I want it to be white by default...

  • @Grupocdsinformática, the theme I’m using makes the font black, I can’t change it in the theme, it has to be in the same customview

  • What I want is: That only in this textview, if I don’t set the textColor in its xml, that the font turns white, if I set, that the font turns the color of what I set... I can not change anything in the theme, it has to be only in this customview

1 answer

2


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

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

if(color==null) {
   setTextColor(Color.WHITE);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.