How to convert a color integer to a hex String in Android?
JavaAndroidStringColorsHexJava Problem Overview
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
Java Solutions
Solution 1 - Java
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));
Solution 2 - Java
Try Integer.toHexString()
Solution 3 - Java
I believe i have found the answer, This code converts the integer to a hex string an removes the alpha.
Integer intColor = -16895234;
String hexColor = "#" + Integer.toHexString(intColor).substring(2);
Note only use this code if you are sure that removing the alpha would not affect anything.
Solution 4 - Java
Here is what i did
int color=//your color
Integer.toHexString(color).toUpperCase();//upercase with alpha
Integer.toHexString(color).toUpperCase().substring(2);// uppercase without alpha
Thanks guys you answers did the thing
Solution 5 - Java
Integer value of ARGB color to hexadecimal string:
String hex = Integer.toHexString(color); // example for green color FF00FF00
Hexadecimal string to integer value of ARGB color:
int color = (Integer.parseInt( hex.substring( 0,2 ), 16) << 24) + Integer.parseInt( hex.substring( 2 ), 16);
Solution 6 - Java
You can use this for color without alpha:
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
or this with alpha:
String hexColor = String.format("#%08X", (0xFFFFFFFF & intColor));
Solution 7 - Java
With this method Integer.toHexString, you can have an Unknown color exception for some colors when using Color.parseColor.
And with this method String.format("#%06X", (0xFFFFFF & intColor)), you'll lose alpha value.
So I recommend this method:
public static String ColorToHex(int color) {
int alpha = android.graphics.Color.alpha(color);
int blue = android.graphics.Color.blue(color);
int green = android.graphics.Color.green(color);
int red = android.graphics.Color.red(color);
String alphaHex = To00Hex(alpha);
String blueHex = To00Hex(blue);
String greenHex = To00Hex(green);
String redHex = To00Hex(red);
// hexBinary value: aabbggrr
StringBuilder str = new StringBuilder("#");
str.append(alphaHex);
str.append(blueHex);
str.append(greenHex);
str.append(redHex );
return str.toString();
}
private static String To00Hex(int value) {
String hex = "00".concat(Integer.toHexString(value));
return hex.substring(hex.length()-2, hex.length());
}
Solution 8 - Java
If you use Integer.toHexString
you will end-up with missed zeros when you convert colors like 0xFF000123
.
Here is my kotlin based solution which doesn't require neither android specific classes nor java. So you could use it in multiplatform project as well:
fun Int.toRgbString(): String =
"#${red.toStringComponent()}${green.toStringComponent()}${blue.toStringComponent()}".toUpperCase()
fun Int.toArgbString(): String =
"#${alpha.toStringComponent()}${red.toStringComponent()}${green.toStringComponent()}${blue.toStringComponent()}".toUpperCase()
private fun Int.toStringComponent(): String =
this.toString(16).let { if (it.length == 1) "0${it}" else it }
inline val Int.alpha: Int
get() = (this shr 24) and 0xFF
inline val Int.red: Int
get() = (this shr 16) and 0xFF
inline val Int.green: Int
get() = (this shr 8) and 0xFF
inline val Int.blue: Int
get() = this and 0xFF
Solution 9 - Java
Fixing alpha issue in Color picker libraries.
if you get an integer value in return when you pick color first convert it into the hex color.
String hexVal = String.format("#%06X", (0xFFFFFFFF &
color)).toUpperCase();
int length=hexVal.length();
String withoutHash=hexVal.substring(1,length);
while (withoutHash.length()<=7)
{
withoutHash="0"+withoutHash;
}
hexVal ="#"+withoutHash;
Solution 10 - Java
String int2string = Integer.toHexString(INTEGERColor); //to ARGB
String HtmlColor = "#"+ int2string.substring(int2string.length() - 6, int2string.length()); // a stupid way to append your color
Solution 11 - Java
use this way in Koltin
var hexColor = "#${Integer.toHexString(ContextCompat.getColor(context, R.color.colorTest))}"
Solution 12 - Java
Simon's solution works well, alpha supported and color cases with a leading "zero" values in R, G, B, A, hex are not being ignored. A slightly modified version for java Color to hex String conversion is:
public static String ColorToHex (Color color) {
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int alpha = color.getAlpha();
String redHex = To00Hex(red);
String greenHex = To00Hex(green);
String blueHex = To00Hex(blue);
String alphaHex = To00Hex(alpha);
// hexBinary value: RRGGBBAA
StringBuilder str = new StringBuilder("#");
str.append(redHex);
str.append(greenHex);
str.append(blueHex);
str.append(alphaHex);
return str.toString();
}
private static String To00Hex(int value) {
String hex = "00".concat(Integer.toHexString(value));
hex=hex.toUpperCase();
return hex.substring(hex.length()-2, hex.length());
}
another solution could be:
public static String rgbToHex (Color color) {
String hex = String.format("#%02x%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() );
hex=hex.toUpperCase();
return hex;
}