Home > OS >  Change TextView color when changing theme
Change TextView color when changing theme

Time:10-29

Question

How can I have the text color change dynamically when the theme changes?

Problem

TextView

Text color dynamically changes when the theme is changed

CodePudding user response:

Solution

Add this style to your TextView and the problem should be solved:

style="@style/Theme.Material3.DynamicColors.Light"

Example

<TextView
            android:text="@string/testText"
      ----> style="@style/Theme.Material3.DynamicColors.Light" <-----
            android:layout_width="400dp"
            android:layout_height="50dp" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:id="@ id/textView"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/>

CodePudding user response:

Yes,You can change the text color when changing the theme.

Ligth Theme

<style name="SimpleTextColor">
        <item name="android:textColor">@color/black</item>
    </style>

Dark/Night Theme

<style name="SimpleTextColor">
        <item name="android:textColor">@color/black</item>
    </style>

Applye in XML

 <TextView
                android:id="@ id/txt_st_name"
                style="@style/SimpleTextColor"  <!--apply like this-->
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"                    
                android:gravity="center"
                android:textSize="22sp"
                /> 
  • Related