71

How to create this shape programmatically?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle">

    <solid android:color="#e67e22"/> 
    <corners
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"/>
</shape>

I've tried this simple function which gets corners, colors and sets that to shape:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.category_header);

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

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

But I got this error:

The method getDrawable() is undefined for the type LinearLayout

Johnny Five
  • 987
  • 1
  • 14
  • 29
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • See this [post](http://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically) or [this](http://stackoverflow.com/questions/7164630/how-to-change-shape-color-dynamically)... – Skizo-ozᴉʞS ツ Feb 18 '15 at 08:02
  • why not use multiple styles? – Droidekas Feb 18 '15 at 08:07
  • @Droidekas i must be have more than 20 style xml file if i can not create custom function to create that – DolDurma Feb 18 '15 at 08:14
  • Did you try using a custom view? – Droidekas Feb 18 '15 at 08:18
  • @Droidekas no sir, can you introducing any tutorials or helo me? by the way Post Updated – DolDurma Feb 18 '15 at 08:24
  • try [this](http://stackoverflow.com/questions/15100660/create-layer-list-with-rounded-corners-programmatically) if you are familiar with android programming – Droidekas Feb 18 '15 at 08:31
  • "The method getDrawable() is undefined for the type LinearLayout" Error Because in XML first, you need to define the background for that LinearLaout. Then only programmatically it able to fetch an override that drawable. – Mihir Trivedi Aug 09 '19 at 12:14

5 Answers5

163

You can do it like this:

public static void customView(View v, int backgroundColor, int borderColor) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
    shape.setColor(backgroundColor);
    shape.setStroke(3, borderColor);
    v.setBackground(shape);
}

See the documentation for the meaning of setCornerRadii params.

You can use this function throughout your app and can put border and background color of your choice.

Johnny Five
  • 987
  • 1
  • 14
  • 29
rahul shah
  • 2,086
  • 2
  • 11
  • 14
32

If what you want is just a simple rounded rectangle, cut the long story short.

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



  • What is the RoundRectShape ?

RoundRectShape specifies an outer (round) rect and an optional inner (round) rect.

// RoundRectShape constructor

   RoundRectShape(float[] outerRadii,
                     RectF inset,
                   float[] innerRadii);
  • outerRadii is an array of 8 radius values, for the outer roundrect. The first two floats are for the top-left corner (remaining pairs correspond clockwise). For no rounded corners on the outer rectangle, just pass null.

enter image description here

For example:

enter image description here

  • inset is a RectF that specifies the distance from the inner rect to each side of the outer rect. For no inner, pass null.

  • innerRadii is an array of 8 radius values, for the inner roundrect. The first two floats are for the top-left corner (remaining pairs correspond clockwise). For no rounded corners on the inner rectangle, pass null. If inset parameter is null, this parameter is ignored.

For example:

enter image description here

ShapeDrawable shape = new ShapeDrawable(
        new RoundRectShape(
            new float[]{20, 20, 20, 20, 20, 20, 20, 20},
            new RectF(10, 20, 10, 20),
            new float[]{40, 40, 40, 40, 40, 40, 40, 40}));
ucMedia
  • 4,105
  • 4
  • 38
  • 46
14

I've created a library which can help to create drawables programmatically.

See here: DrawableToolbox.

With DrawableToolbox, you can create it by:

Drawable drawable = new DrawableBuilder()
        .rectangle()
        .solidColor(0xffe67e22)
        .bottomLeftRadius(20) // in pixels
        .bottomRightRadius(20) // in pixels
//        .cornerRadii(0, 0, 20, 20) // the same as the two lines above
        .build();
Hong Duan
  • 4,234
  • 2
  • 27
  • 50
  • 1
    this library hasn't had a commit since Sept 2018. And its only hosted on jcenter repo, which is deprecated. I recommend using GradientDrawable or another class that is part of the android sdk. – TimCO Mar 03 '22 at 17:37
11

You can also use OVAL shape instead of rectangle:

GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.OVAL);
shape.setColor(Color.WHITE);
shape.setStroke(2, Color.BLACK);
view.setBackground(shape);
Nicolas
  • 6,611
  • 3
  • 29
  • 73
4

If you want to Create

Rounded drawable with Gradient

then use below code.

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

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

    return gd;
}
Mihir Trivedi
  • 1,458
  • 18
  • 39