How to create android shape background programmatically? –

Development issue/problem:

How do I create this form programmatically?

I have tried this simple function that retains angles and colors and adapts them to the shape:

LinearLayout linearLayout = (LinearLayout) findViewById( ;

GradientDrawable Drawable = (GradientDrawable) linearLayout.getDrawable() ;

float[] values = { 0.2f, 0.2f, 0.2f } ;
drawable.setCornerRadii(values) ;

But I have this mistake:

The getDrawable() method is not defined for the type LinearLayout.

How can I solve this problem?

Solution 1:

I’ve created a library that can help you make programmatic drawings.

Take a look at this: Drawable Toolbox.

You can use the DrawableToolbox to create one:

Drawable = new DrawableBuilder()
.bottomLeftRadius(20) // in pixels
.bottomRightRadius(20) // in pixels
// .cornerRadii(0, 0, 20, 20) // in two lines above
.build() ;

Solution 2:

You can do it this way:

public public null and void customView(View v, int backgroundColor, int borderColor) {
GradientDrawable form = new GradientDrawable();
shape.setCornerRadii(new float[] { 8, 8, 0, 0 });
shape.setStroke(3, borderColor);

See the documentation for the values of the setCornerRadii parameters.

You can use this feature throughout your application and set the borders and background color of your choice.

Solution 3:

You can also use an oval shape instead of a rectangle:

GradientDrawable shape = new GradientDrawable() ;
shape.setShape (GradientDrawable.OVAL) ;
shape.setColor (Color.WHITE) ;
shape.setStroke(2, Color.BLACK) ;
view.setBackground(shape) ;

Solution 4:

If you want a simple rounded rectangle, shorten the long story.

float r=8 ;
ShapeDrawable shape = new ShapeDrawable (new RoundRectShape[] { r, r, r },null,null)) ;
shape.getPaint().setColor(Color.RED) ;
view.setBackground(shape) ;

  • What’s RoundRectShape?

The RoundRectShape defines an outer straight line (circular) and an extra inner straight line (circular).

// Builder of the RoundRectShape/

RoundRectShape(float[] externalRadii, insert
float[] internalRadii) ;

  • outerRadii is an array of 8 radius values for external rounding.
    The first two floats are for the upper left corner (the other pairs are clockwise). To avoid rounding off the corners of the outer rectangle, all you have to do is pass a zero.

Give here a description of the image

For example:

Give here a description of the image

  • is a RectF which determines the distance between the inner rectangle and each side of the outer rectangle. If there are no interns, go to zero.
  • innerRadii is an array of 8 radius values for internal rounding. The first two floats are for the upper left corner (the other pairs are clockwise). To make sure there are no rounded corners on the inner rectangle, pass a zero. If the insertion parameter is equal to zero, this parameter is ignored.

For example:

Give here a description of the image

ShapeDrawable shape = new ShapeDrawable(
new RoundRectShape(
new float[]{20, 20, 20, 20},
new RectF(10, 20, 10, 20),
new float[]{40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40) ;

Solution No 5:

If you have a

Rounded pattern with gradient

Then use the code below.

public static GradientDrawable createGradientBackgroundCircular (String topColor, String bottomColor) {
int[] colors = {Color.parseColor(topColor), Color.parseColor(bottomColor)} ;

//create a new gradient
GradientDrawable gd = new gradientDrawable(GradientDrawable.Orientation.TL_BR, colors) ;
gd.setShape(GradientDrawable.OVAL) ;

rd backwards;}

Good luck!

edittext set background programmatically,constraintlayout set background drawable programmatically,android-cardview set background drawable programmatically,android layout set background programmatically,android textview set background drawable programmatically,android set background drawable xml programmatically,android:shape drawable,how to create shape in android studio,android:shape generator,android:shape background color,android arc shape xml,android:shape size,android custom drawable programmatically,create shape drawable programmatically android,android create rectangle shape drawable programmatically,android:shape=ring,android create drawable programmatically from resource

You May Also Like