10

I have a color into the follow format 0xAABBCC as String. I need to convert it into Color, but seems like there is not such method, and all available is returning Integer results, which seems incompatible with Color object;

  • How to create instance of Color class from given HEX string in the format described above ?
0

2 Answers 2

25

use Color.parseColor("#AABBCC");

Sign up to request clarification or add additional context in comments.

4 Comments

parseColor is returning integer. Color c = Color.parseColor( "XXX" ); trowing that Color and int is incompatible.
@BertiKelvin you can use it like int x= Color.parseColor("#AABBCC"); tv.setBackgroundColor(x); you set the color to textview
There is not need to have a instance of the Color class. I think the only reasone for have a public constructor is for the reflection. In fact setTextColor takes as parameter an int
seems like I will have no other choice than using it as Int. Thank You very much. I've got the point.
-1

new Color(Integer.parseInt("AABBCC", 16));

6 Comments

The constructor Color(int) is undefined. :|
Can't help you with that, don't have Android here. Take a look into the Android Docs to find a suitable constructor/factory.
I've tried, but it is going in a dead end with the documentation.
@BertiKelvin : you can parse Current string to int as int color =Integer.parseInt("0xAABBCC", 16) not pass color to setColor method where u want to set it
Did you, by chance, create a class named Color of your own or import maybe a different Color class from somewhere else (I accidentially refactored a class to be named Object once, that was fun)? I don't think Android will be that different in basic things.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.