Convert a hex color value (#ffffff) to an integer value

I get hexadecimal color values ​​from the server (in this form #xxxxxx , example #000000 for black)

How do I convert this value to an integer?

I tried to do Integer.valueOf("0x" + passedColor.substring(1, passedColor.length())) to get even more hextastic 0x000000 result, but here it is not intepreted as int , any other suggestions?

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

I use the Android SDK for my setBackgroundColor(int color) function, which accepts, as you might guess, an integer color value.

This is the OPPOSITE of this question: How to convert an integer of a color to a hexadecimal string in Android?

+59
java android colors numberformatexception
Aug 04 '11 at 1:00
source share
7 answers

The real answer is to use:

Color.parseColor(myPassedColor) in Android, myPassedColor is a hex value, such as #000 or #000000 or #00000000 .

However, this function does not support lowercase hexadecimal values such as #000 .

+133
Jan 26 '13 at 2:48
source share
 Integer.parseInt(myString.replaceFirst("#", ""), 16) 
+9
Aug 04 2018-11-11T00:
source share

I have the same problem that I found a color in the #AAAAAA form and I want to convert it to a form that android can use. I found that you can just use 0xFFAAAAAA so that android can automatically indicate color. Note that the first FF reports alpha . Hope this helps.

+6
Jul 26 '12 at 1:01
source share

I had the same problem. So I was able to solve this. As CQM said, using Color.parseColor () is a good solution to this problem.

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 goal was to change the color of the Button text (Button_C) when I change the color selection from my settings (color_prefs).

+2
Apr 17 '14 at 14:12
source share

The real answer is simple and simple.

 String white = "#ffffff"; int whiteInt = Color.parseColor(white); 
+2
Dec 14 '16 at 5:56
source share

The answer is really simple guys, in android, if you want to convert hex color to int, just use the android class Color , an example is shown below

this is for light gray

 Color.parseColor("#a8a8a8"); 

Here it is, and you will get your result.

+2
Jun 16 '17 at 6:22
source share

Try this, create a resource 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)

Luck

0
Jan 27 '17 at 21:48
source share



All Articles