Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat v22: Set default textcolor for TextView

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?

like image 598
Christopher Avatar asked Jul 15 '15 08:07

Christopher


3 Answers

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.

like image 161
Anand Savjani Avatar answered Oct 31 '22 05:10

Anand Savjani


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>
like image 33
GPack Avatar answered Oct 31 '22 06:10

GPack


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.

like image 2
Mimmo Grottoli Avatar answered Oct 31 '22 07:10

Mimmo Grottoli