Home > database >  how to access primary text color in kotlin fragment
how to access primary text color in kotlin fragment

Time:11-25

I want to change the text color in a pie chart according to the theme. But when I try to access it by ContextCompat.getColor(requireActivity(), android.R.attr.textColorPrimary), it gives an error like this:

android.content.res.Resources$NotFoundException: Resource ID #0x1010036

How can I access it?

CodePudding user response:

If anyone needs it, I found a workaround. First, create this function:

fun Context.getColorThemeRes(@AttrRes id: Int): Int {
     val resolvedAttr = TypedValue()
     this.theme.resolveAttribute(id, resolvedAttr, true)
     return this.getColor(resolvedAttr.resourceId)
}

Then you can access the primary text color:

val textColorPrimary = requireContext().getColorThemeRes(android.R.attr.textColorPrimary)

CodePudding user response:

just use your color attribute like this:

ContextCompat.getColor(requireContext(), R.color.textColorPrimary)
  • Related