I would like to set a default textcolor in my AppTheme, which should be black (not the default Material Design dark grey). The textColor should be overriden by setting a custom style via android:textAppearance-attribute on a UI-element (e.g. TextView).
This is my current setup:
I use AppCompat-Library in my project:
compile 'com.android.support:appcompat-v7:22.2.0'
I defined the following theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/black</item> <!-- All TextViews should have this color as default text color -->
</style>
which is set in the AndroidManifest.xml:
<application
android:name=".core.BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
Furthermore I defined some styles to change the textAppearance of some TextViews:
<style name="App.Heading" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/red</item>
</style>
<style name="App.Heading.Highlighted" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/blue</item>
</style>
Now I have a xml-layout with some TextViews. Some of them should have the default textAppearance (in fact a black text color as defined as android:textColorPrimary in my theme). Some should apply a custom textappearance from my defined styles:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading"
android:textAppearance="@style/App.Heading" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading.Highlighted"
android:textAppearance="@style/App.Heading.Highlighted" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Normal Textview with no style"/>
The first two TextView apply my defined textAppearance, which is fine. But the last TextView has a dark grey textcolor (Material Design default?) and not a black one. If I set the attribute:
<item name="android:textColor">@color/black</item>
in my AppTheme, all TextViews have a black text color. The textcolor defined in my styles (e.g. App.Heading) is no longer recognized.
So my question is: How can I set a default textColor for my TextViews, which could be overridden by setting a textAppearance?
Please define following code in style.xml files which are available in v11 ,v14 and v21 folder.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColor">@android:color/red</item>
</style>
and use this style for every textview.
It is not easy to find the way because there are three styleable android:textColor
attributes, textColorPrimary textColorSecondary and textColorTertiary, and a lot of attributes for base styles android:textAppearance
as small medium etc (now deprecated?).
Anyway, about TextView without style, it seems that it refers as default to the android:textAppearanceSmall
styleable attribute of which value is style TextAppearance.AppCompat.Small
that overrides through Base.TextAppearance.AppCompat.Small
the android:textColor
attribute with ?android:attr/textColorTertiary
value.
Then overriding it like the following will work:
<item name="android:textColorTertiary">@color/black</item>
If I remember well, you cannot set the color of the text using the textAppearance. You need to use a style or a theme to achieve this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With