Search code examples
androidkotlinandroid-jetpack-composematerial-components-androidandroid-dark-theme

color of text changes to white in dark theme


Text(
           style = profileItem
           text = priceStyle(order.total!!),
           modifier = Modifier
                      .padding(start = 2.dp),
     )

val profileItem = TextStyle(
    color = Color.Black,
    fontFamily = FontFamily(Font(R.font.yekanbakhfanum_semibold)),
    fontSize = 14.sp,
    textAlign = TextAlign.Start,
    textDirection = TextDirection.Rtl
)

I am using the code above so I want the text color to be black despite the fact that user phone is in dark mode or not but still it's color changes to white in dark theme

I deleted my darkColors and the theme related to it also copied the lightColors and used them as dark theme too but none of them worked


Solution

    • Create text color for default theme in res/values/colors.xml

      <resources>
          <color name="myTextColor">#000000</color>
      </resources>
      
    • Create text color for night mode in res/values-night/colors.xml

      <resources>
          <color name="myTextColor">#000000</color>
      </resources>
      
    • Use myTextColor color resource in your Text.

      val textStyle = TextStyle(
           color = myTextColor,
           // ...
      )