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));

Solution 4

String colorString=getResources().getString(R.color.redish);

Try this

Share:
15,667
user5294977
Author by

user5294977

Updated on June 05, 2022

Comments

  • user5294977
    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
    user5294977 over 8 years
    context needs to be there... it's within an adapter.
  • IntelliJ Amiya
    IntelliJ Amiya over 8 years
    where you store your color xml ?
  • Carlo Espino
    Carlo Espino about 8 years
    Integer.toHexString(ContextCompat.getColor(context, R.color.redish) with recent versions this worked for me.
  • LukaszTaraszka
    LukaszTaraszka about 7 years
    getColor i deprecated...more
  • user2288580
    user2288580 about 7 years
    Here's another way of doing it:stackoverflow.com/questions/13388493/…
  • rjr-apps
    rjr-apps over 6 years
    This worked for me when the accepted answer didn't. Thanks!
  • PayToPwn
    PayToPwn over 5 years
    The accepted answer was not working for me, but this one does! Kudos!