Android to make Drawable shape programmatically

In Android we can create drawable shape programmatically which can we rectangular or oval shaped

Genarally it is used to create circular button in Android

Button bt1=new Button(getContext());
        RelativeLayout.LayoutParams btrlp=new RelativeLayout.LayoutParams(100,100);
        btrlp.addRule(RelativeLayout.CENTER_IN_PARENT);
        btrlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        bt1.setLayoutParams(btrlp);
        GradientDrawable gd=new GradientDrawable();
        gd.setShape(GradientDrawable.OVAL);
        gd.setColor(Color.RED);
        gd.setStroke(3, Color.RED);
        gd.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
        bt1.setBackground(gd);
       

Leave a Comment