0

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 .

3 Answers3

1

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

Mehdi bahmanpour
  • 574
  • 7
  • 21
0

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

0

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
Alvin Tom
  • 119
  • 4