Get color resource as string
15,667
Solution 1
I think you missed #
Color.parseColor("#"+Integer.toHexString(ContextCompat.getColor(context, R.color.redish)))
Solution 2
Updated answer:
String colorHex = "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.colorPrimary) & 0x00ffffff);
Solution 3
context.getResources().getColor(R.color.redish));
Author by
user5294977
Updated on June 05, 2022Comments
-
user5294977 almost 2 years
I'm trying to use
Color.parseColor()
on a color resource:<color name="redish">#FF0000</color>
I've tried this, but it gives me the error Unknown color:
Color.parseColor(Integer.toHexString(context.getResources().getColor(R.color.redish)))
How do I convert the color resource to a
String
properly? -
user5294977 over 8 years
context
needs to be there... it's within an adapter. -
IntelliJ Amiya over 8 yearswhere you store your color xml ?
-
Carlo Espino about 8 years
Integer.toHexString(ContextCompat.getColor(context, R.color.redish)
with recent versions this worked for me. -
LukaszTaraszka about 7 yearsgetColor i deprecated...more
-
user2288580 about 7 yearsHere's another way of doing it:stackoverflow.com/questions/13388493/…
-
rjr-apps over 6 yearsThis worked for me when the accepted answer didn't. Thanks!
-
PayToPwn over 5 yearsThe accepted answer was not working for me, but this one does! Kudos!