How to change default text color using custom theme?

What I'm trying should be quite easy with themes, but I can't find out how to: I want all text to be white by default in my app. I created a custom theme in theme.xml:

<style name="Theme" parent="@android:Theme">
</style>

<style name="TextAppearance.Theme" parent="@android:TextAppearance.Theme">
    <item name="android:textColor">#ffffffff</item>
</style>

and set it for the whole application:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme">

But labels are still black. What's missing?

PS: How can I additionally define styles for different text sizes, to be applied per widget? Is something like that correct?

<style name="Theme.smallText">
    <item name="android:textSize">12dp</item>
</style>

update

I took a look at themes.xml in Android SDK, it shows how to set the text style for a theme:

<item name="textAppearance">@android:style/TextAppearance</item>

In my case it should work with this definition:

<style name="Theme" parent="@android:Theme">
    <item name="android:textAppearance">@style/MyText</item>
</style>

<style name="MyText" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
</style>

However, it is still not working.

Here's another post about this same issue.


Solution 1:

In your Manifest you need to reference the name of the style that has the text color item inside it. Right now you are just referencing an empty style. So in your theme.xml do only this style:

<style name="Theme" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
</style>

And keep you reference to in the Manifest the same (android:theme="@style/Theme")

EDIT:

theme.xml:

<style name="MyTheme" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffffffff</item>
    <item name="android:textSize">12dp</item>
</style>

Manifest:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/MyTheme">

Notice I combine the text color and size into the same style. Also, I changed the name of the theme to MyTheme and am now referencing that in the Manifest. And I changed to @android:style/TextAppearance for the parent value.

Solution 2:

When you create an App, a file called styles.xml will be created in your res/values folder. If you change the styles, you can change the background, text color, etc for all your layouts. That way you don’t have to go into each individual layout and change the it manually.

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppBaseTheme" parent="@android:style/Theme.Light">
    <item name="android:editTextColor">#295055</item> 
    <item name="android:textColorPrimary">#295055</item>
    <item name="android:textColorSecondary">#295055</item>
    <item name="android:textColorTertiary">#295055</item>
    <item name="android:textColorPrimaryInverse">#295055</item>
    <item name="android:textColorSecondaryInverse">#295055</item>
    <item name="android:textColorTertiaryInverse">#295055</item>

     <item name="android:windowBackground">@drawable/custom_background</item>        
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

parent="@android:style/Theme.Light" is Google’s native colors. Here is a reference of what the native styles are: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml

The default Android style is also called “Theme”. So you calling it Theme probably confused the program.

name="Theme.AppBaseTheme" means that you are creating a style that inherits all the styles from parent="@android:style/Theme.Light". This part you can ignore unless you want to inherit from AppBaseTheme again. = <style name="AppTheme" parent="AppBaseTheme">

@drawable/custom_background is a custom image I put in the drawable’s folder. It is a 300x300 png image.

#295055 is a dark blue color.

My code changes the background and text color. For Button text, please look through Google’s native stlyes (the link I gave u above).

Then in Android Manifest, remember to include the code:

<application
android:theme="@style/Theme.AppBaseTheme">