Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color of the text is not changing when style including textColor is applied to textAppearance of textView

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

like image 544
Vipul Kumar Avatar asked Jul 10 '17 09:07

Vipul Kumar


People also ask

How do I change text color in XML?

TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

What is TextAppearance in android?

TextAppearance allows you to define text-specific styling while leaving a View 's style available for other uses. Note, however, that if you define any text attributes directly on the View or in a style, those values would override the TextAppearance values.


1 Answers

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.

like image 132
Yash Avatar answered Oct 02 '22 01:10

Yash