I am trying to convert a hex value to an int so I can create a new color drawable. I'm not sure if this is possible, but according to the documentation, it should. It plainly asks for
public ColorDrawable (int color)
Added in API level 1 Creates a new ColorDrawable with the specified color.
Parameters color The color to draw.
So, my code isn't working because I'm getting an Invalid int: "FF6666" error. Any ideas?
int decode = Integer.decode("FF6666");
ColorDrawable colorDrawable = new ColorDrawable(decode);
By followingthe above advice,to be a summary of this question:
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ce9b2c"));`
ColorDrawable colorDrawable = new ColorDrawable(0xFFCE9B2C); Note there is 8 hex digits, not 6 hex digit,which no work. Case all
ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(mContext,R.color.default_color));
Selecting up to you!
For using with ContextCompat and rehuse the color you can do something like this:
I think you have to use :
It should be like this...
Note I used 8 hex digits, not 6 hex digit . which add to transparency
Let's say that we have a color #8080000.
1) Hex to int conversion
2) Set background
Since you're talking about hex you have to start with
0x
and don't forget the opacity.So basically: 0xFFFF6666
You can also create a new colors.xml file into /res and define the colors like:
and simply get the color defined in R.color.mycolor