95

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);
Saleh
  • 1,819
  • 1
  • 17
  • 44
c_idle
  • 1,448
  • 4
  • 22
  • 40
  • if one of the answers provided solved your answer, think of validating it as an accepted answer so the other knows your problem is solved. – HpTerm Oct 17 '13 at 11:30
  • There is a nice utility class android.graphics.Color that have a method parseColor. Try using it instead and don't forget to append the sharp (#) prefix to your colors string representation, e.g. #FF6666 – Игорь Комаров Feb 27 '18 at 16:01

7 Answers7

191

Since you're talking about hex you have to start with 0x and don't forget the opacity.

So basically: 0xFFFF6666

ColorDrawable cd = new ColorDrawable(0xFFFF6666);

You can also create a new colors.xml file into /res and define the colors like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="mycolor">#FF6666</color>
</resources>

and simply get the color defined in R.color.mycolor

getResources().getColor(R.color.mycolor)
Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • 22
    what i needed is `new ColorDrawable(getResources().getColor(R.color.red)))` thanks – shareef Dec 22 '15 at 18:55
  • 9
    ContextCompat.getColor(getContext(),R.color.red) for compatiability – loshkin Apr 07 '16 at 14:37
  • can we change background color by using this **cd** which is colorDrawable type variable –  Jul 26 '16 at 08:53
  • On a side note, you can use ColorDrawable's for objects that require a Drawable such as DividerItemDecoration.setDrawable(); instead of using an actual drawable layout you can pass a ColorDrawable to simply change the divider's color. – 6rchid Feb 05 '20 at 18:29
30

For using with ContextCompat and rehuse the color you can do something like this:

ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.white));
JpCrow
  • 4,881
  • 4
  • 32
  • 46
10

It should be like this...

ColorDrawable cd = new ColorDrawable(0xffff6666);

Note I used 8 hex digits, not 6 hex digit . which add to transparency

Community
  • 1
  • 1
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
7

By followingthe above advice,to be a summary of this question:

  1. ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#ce9b2c"));`

  2. ColorDrawable colorDrawable = new ColorDrawable(0xFFCE9B2C); Note there is 8 hex digits, not 6 hex digit,which no work. Case all

  3. ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(mContext,R.color.default_color));

Selecting up to you!

BertKing
  • 513
  • 5
  • 13
4

I think you have to use :

public static int parseColor (String colorString)

Added in API level 1 Parse the color string, and return the corresponding color-int. If the string cannot be parsed, throws an IllegalArgumentException exception. Supported formats are: #RRGGBB #AARRGGBB red, blue, green, black, white, gray, cyan, magenta, yellow, lightgray, darkgray, grey, lightgrey, darkgrey, aqua, fuschia, lime, maroon, navy, olive, purple, silver, teal

HpTerm
  • 8,151
  • 12
  • 51
  • 67
1

This is how I converted a Hex color to int and applied to a Background of a View

Let's say that we have a color #8080000.

1) Hex to int conversion

int myColor = Color.parseColor("#808000");

2) Set background

view.setBackgroundColor(context.getColor(myColor));
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
0

Xamarin/Maui :

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);      

        Window.SetBackgroundDrawable(new ColorDrawable(ColorExtensions.ToAndroid(Colors.Black)));
    }
Nick Kovalsky
  • 5,378
  • 2
  • 23
  • 50