Java – How to convert a color integer to a hex String in Android

androidcolorshexjavastring

I have an integer that was generated from an android.graphics.Color

The Integer has a value of -16776961

How do I convert this value into a hex string with the format #RRGGBB

Simply put: I would like to output #0000FF from -16776961

Note: I do not want the output to contain an alpha and i have also tried this example without any success

Best Answer

The mask makes sure you only get RRGGBB, and the %06X gives you zero-padded hex (always 6 chars long):

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));