Home >Java >javaTutorial >How to Convert Color Integers to Hex Strings in Android?
Converting Color Integers to Hex Strings in Android
When working with colors in Android, you may encounter situations where you need to convert color integers to hex strings. Consider the scenario where you have an integer value generated from an android.graphics.Color, such as -16776961, and you want to represent it as a hex string in the format #RRGGBB.
To achieve this, the key is to extract the red, green, and blue (RGB) components from the integer value. You can do this by using bitwise operations. The following code snippet demonstrates how to convert the color integer to a hex string:
<code class="java">String hexColor = String.format("#%06X", (0xFFFFFF & colorInt));</code>
Using this method, you can successfully convert the sample integer value of -16776961 to the hex string #0000FF, which represents pure blue.
The above is the detailed content of How to Convert Color Integers to Hex Strings in Android?. For more information, please follow other related articles on the PHP Chinese website!