android - Custom TextView with TextAppearance cannot have textColor overridden in XML -


i'm trying create custom implementation of textview can used encapsulate of desired attributes of text displayed.

what i've managed far is:

  • apply custom font.
  • apply textappearance includes things letterspacing , textcolor.

this works expected but when try change colour of text in xml new text colour ignored , colour of text remains default i've setup in textappearance. appears textappearance overrides properties of textview provided in xml.

question:

how create custom textview default properties can modified in xml (mainly textcolor)? consider custom textview exists in separate module project it'll used in.

code:

customtextview

public class mytextview extends appcompattextview {  public mytextview(context context) {     super(context);     init(context); }  public mytextview(context context, attributeset attrs) {     super(context, attrs);     init(context); }  public mytextview(context context, attributeset attrs, int defstyleattr) {     super(context, attrs, defstyleattr);     init(context); }  private void init(context context) {     settextappearance(context, r.style.mytext);     settypeface(fontcache.futurabookreg(getcontext()));    } } 

style

<style name="mytext" parent="textappearance.appcompat">     <item name="android:textcolor">@color/default_text_colour_selector</item>     <item name="android:textsize">28sp</item>     <item name="android:letterspacing">0.04</item>     <item name="android:linespacingextra">8sp</item> </style> 

fontcache (included completeness. issue persists without custom fonts)

class fontcache {  private static final string my_font = "font/my_font.ttf";  private static map<string, typeface> fontmap = new hashmap<>();  static typeface myfont(context context) {     return getfont(context, my_font); }  private static typeface getfont(context context, string fontname) {     if (fontmap.containskey(fontname)) {         return fontmap.get(fontname);     } else {         typeface tf = typeface.createfromasset(context.getassets(), fontname);         fontmap.put(fontname, tf);         return tf;     }    } } 

attempt colour textview in xml

<com.example.text.mytext         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textcolor="@color/new_text_colour"         android:text="hello" /> 


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -