6

I want to reduce my xml code repetition. So I made some standard styles for text in textView. We can apply styles under 'style' attribute as well as 'android:textAppearance' attribute in textView.

Below are some styles I made for text appearance-

<style name="Grey">
    <item name="android:textColor"> #333333 </item>
</style>

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:textColor"> #00FF00 </item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">20sp</item>
</style>

When I apply these styles under 'textAppearance' attribute the color of the text is not changing in none of the above styles. It's working under 'style' attribute of textView.

//textColor not working
<TextView
    android:id="@+id/profile_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Full Name"
    android:textAppearance="@style/CodeFont"/>

//textColor working
<TextView
    android:id="@+id/profile_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Full Name"
    style="@style/CodeFont"/>

I want them to work under 'textAppearance' attribute so that I can apply some other style under 'style' attribute. And according to android documentation we can apply textColor styles under 'textAppearance' attribute.

Please suggest some solution to this. Thanks

Vipul Kumar
  • 653
  • 8
  • 14
  • 1
    textColor is most likely overriden by default TextView style, as style has precedence over textAppearance. To check this you can set android:textColor="@null" before the line android:textAppearance="@style/CodeFont". – Kurovsky Feb 10 '20 at 12:34

3 Answers3

5

Try setting the text color in your widget as null like this:

<TextView
android:id="@+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="@null"  //add this line
android:textAppearance="@style/CodeFont"/>

Also, I think you should try to Invalidate cache and Restart Android Studio. Import and linking issues can be solved like this sometimes.

Yash
  • 3,438
  • 2
  • 17
  • 33
2

This snippet works for me

 <style name="fonts" parent="TextAppearance.AppCompat">
    <item name="android:textColor">#245546</item>
    <item name="android:textSize">30dp</item>
</style>

and textview is

  <TextView
    android:id="@+id/sample"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to stackoverflow"
    android:textAppearance="@style/fonts"/>
Mohammed Farhan
  • 1,120
  • 8
  • 14
0

Sometimes if you don't give android:textColor in styles you can have this issue that text color won't appear in your TextView, So the solution is to just give completely <item name="android:textColor">@color/yourColor</item> in styles rather than this <item name="textColor">@color/yourColor</item>.. your problem would be resolved :)