Reputation: 21
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 .
Upvotes: 0
Views: 2130
Reputation: 119
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
Upvotes: 0
Reputation: 614
Use setBackgroundDrawable()
instead of setBackground()
And if you want to set a color as background use setBackgroundColor()
.
Upvotes: 1
Reputation: 164
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
Upvotes: 0