How to change the textColor for all widgets in the App when the theme is Material3

Viewed 186

I want to change the default textColor of all text in my App. e.g. for all buttons, textViews, EditText's etc. in my App. Before moving to Material3, this was done using:

themes.xml

<style name="MyThemeName" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/black</item>

Now it doesn't work anymore. How do I do it now? The project is a default Android project:

<style name="Theme.MyApplication" parent="Theme.Material3.DayNight.NoActionBar">
    <!-- This is not working -->
    <item name="android:textColor">@color/black</item>

    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

I can do it like this, but that is not the answer I'm looking for:

<style name="BaseStyle" parent="android:Widget.Material.Light.Button.Borderless"/>

<style name="Widget.Button.MyCompanyName" parent="BaseStyle">
    <item name="android:textColor">@android:color/black</item>
</style>

The above button style I only define the textColor when it differs from the default color that I want.

2 Answers

The text color of the elements will change based on what type of element it is being shown on. for example if you are using the material button by default the button color will be "colorPrimary" and the button text color will be "colorOnPrimary". and similar to this if an element color uses the "colorBackground" its text color will be "colorOnBackground".

in my case i was able to control the text color of all the elements i use by adding a value for the following styles:

<item name="colorOnPrimary">#000000</item>
<item name="colorOnPrimaryContainer">#000000</item>
<item name="colorOnSecondary">#000000</item>
<item name="colorOnSecondaryContainer">#000000</item>
<item name="colorOnBackground">#000000</item>
<item name="colorOnSurface">#000000</item>
<item name="colorOnSurfaceVariant">#000000</item>
<item name="android:textColorPrimary">#000000</item>
<item name="titleTextColor">#000000</item>

you can read more about this in "Typography and iconography colors" section Here

Does the below work? Not sure textViewStyle covers ALL texts. You might need some extra things like <item name="materialButtonStyle"> too.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="android:textViewStyle">@style/MyTextView</item>
</style>

<style name="MyTextView" parent="Widget.MaterialComponents.TextView">
    <item name="android:textColor">@android:color/black</item>
</style>


Related