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.
– Grupo CDS Informática
Have you tried removing setTextColor(Color.WHITE); ?! Hehe
– viana
@white, I want it to be white by default...
– felipe.rce
@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
– felipe.rce
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
– felipe.rce