0

I have a TextView defined in xml. The textColor attribute fieild in the Android Studio properties pannel shows [default] "@android:color/secondary_text_material_light".

How to get this default color programmically to reset a TextView color when needed?

PS: I've read through this post but it does not seem to answer my question.

yaugenka
  • 2,602
  • 2
  • 22
  • 41
  • Why doesn't the linked post answer your question? – Prateek Kumar May 13 '20 at 13:46
  • is the problem getting the color from resources or setting the text color programmatically? – Idan Damri May 13 '20 at 13:57
  • @PrateekKumar the accepted answer in that post suggests saving the default color before changing it which I consider a workarround rather than a solution. Alternative anwers there suggest using `getColor` functions with a resource id, but `android.R.color.secondary_text_material_light` throughs unresolved reference for me to some reason. – yaugenka May 13 '20 at 16:03

2 Answers2

0

You can get a color defined as a color resource programmatically with this line of code.

ContextCompat.getColor(context, android.R.color.secondary_text_material_light);

I haven´t tested, but i think it should work. I hope i could help.

EDIT 1: If the problem is to set the text color...

You can change it via the textView.setTextColor() method. The full code could look like this:

your_text_view.setTextColor(ContextCompat.getColor(this, android.R.color.secondary_text_material_light));
JonasPTFL
  • 189
  • 4
  • 15
  • `android.R.color.secondary_text_material_light` throughs unresolved reference to me to some reason – yaugenka May 13 '20 at 15:57
  • Ok instead of `android.R.color.secondary_text_material_light` you could use the private value `R.color.secondary_text_default_material_light` which is the direct hex color from `android.R.color.secondary_text_material_light`, but this is a workaround too. Maybe a better than save the color value before changing as in the other SO post. I dont know... But do you get an unsolved reference error with this resource value too? – JonasPTFL May 13 '20 at 16:22
  • The second resource does work but as you have noted yourself, it throughs the private resource warning. – yaugenka May 13 '20 at 16:40
  • 1
    Ok, last approach, i found recently... You can use `android.R.color.tab_indicator_text` as the color is the same. This method gives you no warnings, but is still a workaround. But maybe a better one. I dont know any other "solutions" as `android.R.color.secondary_text_material_light` doesnt work. – JonasPTFL May 13 '20 at 17:13
0

I ended up with the following workaround solution.

textView.setTextColor(TextView(context).textColors)
yaugenka
  • 2,602
  • 2
  • 22
  • 41