Convert hex color value ( #ffffff ) to integer value

JavaAndroidColorsNumberformatexception

Java Problem Overview


I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black)

How do I convert this to an integer value?

I tried doing Integer.valueOf("0x" + passedColor.substring(1, passedColor.length())) to get an even more hextastic 0x000000 result, but this isn't intepreted as an int here, any other suggestions?

I receive an error: 08-03 21:06:24.673: ERROR/AndroidRuntime(20231): java.lang.NumberFormatException: unable to parse '0x00C8FBFE' as integer

I am using the Android SDK for their setBackgroundColor(int color) function, which takes - as you might have guessed - an integer color value.

This is the OPPOSITE of this question: https://stackoverflow.com/questions/6539879/how-to-convert-a-color-integer-to-a-hex-string-in-android

Java Solutions


Solution 1 - Java

The real answer is to use:

Color.parseColor(myPassedColor) in Android, myPassedColor being the hex value like #000 or #000000 or #00000000.

However, this function does not support shorthand hex values such as #000.

Solution 2 - Java

Answer is really simple guys, in android if you want to convert hex color in to int, just use android Color class, example shown as below

this is for light gray color

Color.parseColor("#a8a8a8");

Thats it and you will get your result.

Solution 3 - Java

Integer.parseInt(myString.replaceFirst("#", ""), 16) 

Solution 4 - Java

I have the same problem that I found some color in form of #AAAAAA and I want to conver that into a form that android could make use of. I found that you can just use 0xFFAAAAAA so that android could automatically tell the color. Notice the first FF is telling alpha value. Hope it helps

Solution 5 - Java

The real answer is this simplest and easiest ....

String white = "#ffffff";
int whiteInt = Color.parseColor(white);

Solution 6 - Java

I was facing the same problem. This way I was able to solved it. As CQM said, using Color.parseColor() is a good solution to this issue.

Here is the code I used:

this.Button_C.setTextColor(Color.parseColor(prefs.getString("color_prefs", String.valueOf(R.color.green))));

In this case my target was to change the Button's text color (Button_C) when I change the color selection from my Preferences (color_prefs).

Solution 7 - Java

Based on CQM's answer and on ovokerie-ogbeta's answer to another question I've come up with this solution:

if (colorAsString.length() == 4) { // #XXX
    colorAsString = colorAsString.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");
}
        
int color = Color.parseColor(colorAsString);

Solution 8 - Java

if you can pass the values as static const, you can convert the hex value to an Android (android.graphics.Color) using this online converter and put the color reference in the const, ie: color converter give me this value for this color #EE5670 = 0xFFEE5670.

static const Color redColor = const Color(0xFFEE5670);

https://convertingcolors.com/hex-color-EE5670.html?search=#EE5670

Solution 9 - Java

Get Shared Preferences Color Code in String then Convert to integer and add layout-background color:

    sharedPreferences = getSharedPreferences(mypref, Context.MODE_PRIVATE);
    String sw=sharedPreferences.getString(name, "");
    relativeLayout.setBackgroundColor(Color.parseColor(sw));

Solution 10 - Java

Try this, create drawable in your resource...

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/white"/>
    <size android:height="20dp"
        android:width="20dp"/>
</shape>

then use...

 Drawable mDrawable = getActivity().getResources().getDrawable(R.drawable.bg_rectangle_multicolor);
mDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
mView1.setBackground(mDrawable);

with color... "#FFFFFF"

if the color is transparent use... setAlpha

mView1.setAlpha(x); with x float 0-1 Ej (0.9f)

Good Luck

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCQMView Question on Stackoverflow
Solution 1 - JavaCQMView Answer on Stackoverflow
Solution 2 - JavaAshana.JackolView Answer on Stackoverflow
Solution 3 - JavaMike SamuelView Answer on Stackoverflow
Solution 4 - JavaFeiView Answer on Stackoverflow
Solution 5 - Javauser7294802View Answer on Stackoverflow
Solution 6 - JavaRunningWheelsView Answer on Stackoverflow
Solution 7 - JavaMikeLView Answer on Stackoverflow
Solution 8 - JavaALEXANDER LOZANOView Answer on Stackoverflow
Solution 9 - JavaChoudry ZamanView Answer on Stackoverflow
Solution 10 - JavaGaston CufreView Answer on Stackoverflow