Search code examples
android-canvaspaintandroid-custom-viewandroid

Android color code WHITE conversion to hexadecimal


I am working with hybrid android application where i need to pass color code from java class to html. My HTML code is expecting "#RGB", where as in my android code declared some color as Color.WHITE and Color.TRANSPARENT whose equivalent int values are -1 and 0

How can i convert Color.WHITE i.e (-1) to some #FFFFFF?

I know Color.parseColor(#FFFFFF) but i need reverse one.


Solution

  • Since color is actually an integer, you can easily convert it to hexadecimal with String.format. It seems you want to ignore the alpha channel so you can filter it out:

    String.format("#%06X", color & 0xffffff);