I'm trying to create a CustomButton dynamically for java code with 10dp padding and 10dp rounded corners. I wrote the following:
public class CustomButton extends Button {
private GradientDrawable gd;
private ShapeDrawable sd;
private LayerDrawable ld;
private StateListDrawable sld;
public CustomButton(Context context) {
super(context);
gd = new GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[]{Color.parseColor("#39D100"), Color.parseColor("#369212")});
gd.setCornerRadius(10);
sd = new PaintDrawable();
sd.setPadding(10, 10, 10, 10);
ld = new LayerDrawable( new Drawable[]{sd, gd} );
sld = new StateListDrawable();
sld.addState(new int[]{0,1}, ld);
setBackgroundDrawable(sld);
}
}
If I add only the GradientDrawable or only the ShapeDrawable to StateListDrawable it works correctly, but if I put both into the LayerDrawable the padding and corner parts will have a black background and not transparent as expected.
ScreenShot: