java.lang.IllegalArgumentException: Unknown color on use static color string

after create this class

public class MyColors {
public static String COLOR_APPLICATION = "#FF445FC7";
public static String COLOR_APPLICATION_OPACITY = "#cbb19155";
public static String PRIMARY_COLOR = "#211d1c";
public static String NAVBAR_COLOR = "#0c0c0c";
public static String BACKGROUND_COLOR = "#cbb19155";}

then create:

public class MyDrawables {

public static Drawable txt_shape = new DrawableBuilder()
        .rectangle().bottomLeftRadius(9).bottomRightRadius(9)
        .solidColor(Color.parseColor(MyColors.COLOR_APPLICATION))
        .build();

public static Drawable text_view_center_shape = new DrawableBuilder()
        .rectangle().solidColor(Color.parseColor("#009c3bbc"))
        .strokeColor(Color.parseColor("#9f9f9f")).strokeWidth(1)
        .build();}

gettig that exception when using myDrawable like :

serviceTxt.setBackground(MyDrawables.txt_shape);

what can I do to avoid this crash ?

P.S: application does'nt crashing when create my drawable in activity and use it .

Use setBackgroundDrawable() instead of setBackground() And if you want to set a color as background use setBackgroundColor().

[Android] java.lang.IllegalArgumentException:Unknown color, I am using hex color - #000000 12-03 19:05:07.546: 12-03 19:05:07.546: E/​AndroidRuntime(23327): java.lang.IllegalArgumentException: Unknown color public static int getColorFromHexString(String hexString){ 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’ Your method probably returns a string that doesn’t start with a #.

You can use static methods like:

    public static Drawable txtShape (){
    return new DrawableBuilder()
    .rectangle().bottomLeftRadius(9).bottomRightRadius(9)
    .solidColor(Color.parseColor(MyColors.COLOR_APPLICATION))
    .build();}

Or create all shapes you need in drawable folder in res directory like .xml file and refer to them like R.drawable.text_shape.

And it will be better to store your colors in res/values/colors.xml

Getting an 'unknown color' error on logcat, To easily encode color ints, it is recommended to use the static methods argb(int,​int,int,int) in Android O to store colors in different ColorSpace , with more precision than color ints. Parse the color string, and return the corresponding color-int. IllegalArgumentException, If the encoded color space is invalid or unknown  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

Not very sure about this, but pls recheck your color string format. If im not mistaken, color string refers to color hex code which is "#xxxxxx"

public class MyColors {
public static String COLOR_APPLICATION = "#FF445FC7"; //<= recheck this
public static String COLOR_APPLICATION_OPACITY = "#cbb19155"; //<= recheck this
public static String PRIMARY_COLOR = "#211d1c"; //<= this is the right format
public static String NAVBAR_COLOR = "#0c0c0c";
public static String BACKGROUND_COLOR = "#cbb19155";} //<= recheck this

Color, To easily encode color ints, it is recommended to use the static methods argb(int, int, int, int) in Android O to store colors in different ColorSpace , with more precision than color ints. Parse the color string, and return the corresponding color-int. IllegalArgumentException, If the encoded color space is invalid or unknown  Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number at java.text.DecimalFormat.format(Unknown Source) at java.text.Format.format(Unknown Source) at java.text.MessageFormat.subformat(Unknown Source) at java.text.MessageFormat.format(Unknown Source) at java.text.Format.format(Unknown Source) at

titanium_mobile/TiColorHelper.java at master · appcelerator , Please see the LICENSE included with this distribution for details. */ import org.​appcelerator.kroll.common.Log;. import android.graphics.Color; This class contain utility methods that converts a String color, like "red", into its private static final String TAG = "TiColorHelper"; Log.w(TAG, "Unknown color: " + value​);. public IllegalArgumentException(String message, Throwable cause) Constructs a new exception with the specified detail message and cause. Note that the detail message associated with cause is not automatically incorporated in this exception's detail message.

graphics/java/android/graphics/Color.java, import java.util. Colors are represented as packed ints, made up of 4 bytes: alpha, red, Each component ranges between 0..255 with 0 public static final int LTGRAY = 0xFFCCCCCC; If the string cannot be parsed, throws an IllegalArgumentException throw new IllegalArgumentException("Unknown color");. static Colour[] getAllColours() Returns all available colours - used when generating the default palette: int: getDefaultBlue() Deprecated. use getDefaultRGB instead: int: getDefaultGreen() Deprecated. use getDefaultRGB instead: int: getDefaultRed() Deprecated. use getDefaultRGB instead RGB: getDefaultRGB() Returns the default RGB of the colour: java.lang.String

Uses of Class java.lang.IllegalArgumentException (Java Platform SE , Characteristics · Collectors · Color · ColorChooserComponentFactory · ColorChooserUI · ColorConvertOp setAsText(String text) Uses of IllegalArgumentException in java.beans.beancontext Gets the value of a static or instance boolean field. Unchecked exception thrown when an unknown conversion is given. Using Enum.valueOf is great when you know the input is valid. However, if you pass in an invalid name, an exception will be thrown. In some cases, this is fine. Oftentimes. we would prefer to just ignore it and return null.

Comments
  • please post the exception.
  • java.lang.ExceptionInInitializerError: java.lang.IllegalArgumentException: Unknown color @kelalaka
  • can you test with small application with your string? stackoverflow.com/questions/8225185/…
  • still not working , I won't to create in drawable because the colors are dynamic in application