Create a new color drawable

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)

  • android user agent
  • Setting “task affinity” programmatically
  • Single XML, Multiple Activity & there Performance
  • Executing Groovy scripts embed in Java on runtime for Android
  • Expand TextView with wrap_content until the neighbor view reaches the end of the parent
  • How to delete the published app from developer console android
  • Added in API level 1 Creates a new ColorDrawable with the specified

    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);

    Related posts:

    Remove shadow below actionbar
    Expand clickable area of an ImageView by using padding?
    Android Spinner Size very large
    gradle build fails on lint task
    To use layout_marginLeft in a button style applied as a theme?
    How do I set the minimum api level for projects in Android Studio?
  • How to align views in the middle of another views baseline?
  • LayoutManager for RecyclerView Grid with different cell width
  • How to always run a service in the background?
  • android : set textView style at runtime
  • Android convert date and time to milliseconds
  • Android: Streaming audio over TCP Sockets
  • 4 Solutions collect form web for “Create a new color drawable”

    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"?>
        <color name="mycolor">#FF6666</color>

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


    It should be like this…

    ColorDrawable cd = new ColorDrawable(0xffff6666);

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

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

    ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(this, R.color.white));

    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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.