How to convert a color integer to a hex String in Android?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Android colors are often stored as 32-bit integers in ARGB format. If you want to display them, log them, or serialize them in a human-readable way, you usually need to convert that integer into a hexadecimal string such as #FF336699.
The conversion itself is simple, but you need to decide whether to include alpha and whether the output should be uppercase, lowercase, or prefixed with #. Those formatting choices are usually more important than the integer math.
Convert ARGB Int to Hex String
A standard Android color integer includes alpha, red, green, and blue channels. The easiest way to turn it into hex is String.format:
This produces:
The %08X means:
- use hexadecimal
- use uppercase letters
- pad to 8 characters with leading zeroes
That is the usual answer when you want the full ARGB form.
Output RGB Only
Sometimes you do not want alpha in the final string. In that case, mask off the upper byte:
This produces:
That is useful when the receiving code assumes opaque colors or only wants the visible RGB portion.
Kotlin Version
The same formatting works cleanly in Kotlin:
If you are already in Kotlin, this is often the simplest direct solution.
Use Color Helpers When You Need Components
If you need to inspect channels separately before formatting, Android's Color helpers make that clearer:
This is useful when the color did not start as a packed integer literal and you want explicit control over the output order.
Be Careful with Signed Ints
Android color ints are often negative when viewed as ordinary Java integers because the highest bit may be set. That does not mean the color is wrong.
For example:
The decimal value may be negative, but the formatted hex string will still correctly show the ARGB bytes.
Pick One Output Format and Stay Consistent
Teams often run into confusion because one layer stores #RRGGBB while another expects #AARRGGBB. Decide on one format for logging, persistence, and API boundaries, then keep the conversion logic in one place. That avoids subtle bugs where a transparent color is accidentally treated as fully opaque, or the first byte is misread as red instead of alpha.
Common Pitfalls
- Forgetting whether the output should include alpha.
- Using
%06Xwhen the color actually needs full ARGB output. - Being surprised that a color int prints as a negative decimal number.
- Forgetting the
#prefix when the result will be reused in color-parsing APIs. - Manually shifting channels when
String.formatandColorhelpers already solve the problem clearly.
Summary
- Android colors are usually packed ARGB integers.
- Use
String.format("#%08X", color)for full ARGB output. - Use
0xFFFFFF & colorwith%06Xwhen you want RGB only. - Negative decimal color values are normal for some ARGB ints.
- Use
Color.alpha,Color.red,Color.green, andColor.bluewhen channel-level control is useful.

