android - How to set corner radius programmatically?

P.Lorand picture P.Lorand · May 19, 2017 · Viewed 14.6k times · Source

I have two ListViews (leftList, rightList). I also have one TextView which I use as row view in both of them. I have a rectangle drawable shape and set it as background to the TextView.

I would like to change this shape and have rounded corners on the left.

What I tried :

       GradientDrawable gradientDrawable = new GradientDrawable();
       // gradientDrawable.setCornerRadius(30);
        ((GradientDrawable)gradientDrawable.mutate()).setCornerRadius(30);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            viewHolderPattern.digits.setBackground(gradientDrawable);
        }

I have created a new layout in drawable with the right corner radius set and set that to the textView with setBackgroundRescource but still didn't work.

The TextView that I use as items in both listViews

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/digitsTextView"
    android:textSize="15dp"
    android:paddingTop="7dp"
    android:paddingBottom="8dp"
    android:fontFamily="monospace"
    android:textColor="@color/selected_items"
    android:background="@drawable/digital_text_shape">
</TextView>

Shape layout digital_text_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="@color/orange" />
    <solid android:color="@color/orange" />
    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="0dp"
        />
    <padding
        android:bottom="0dp"
        android:left="20dp"
        android:right="0dp"
        android:top="0dp" />
</shape>

Left list and Right list

<!-- Left ListView -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                        <ListView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center_horizontal"
                            android:id="@+id/left_listView"
                            android:divider="@android:color/transparent"
                            android:dividerHeight="0.1sp"
                            android:choiceMode="singleChoice"

                            >
                        </ListView>
                </LinearLayout>

                <!-- Right ListView -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                        <ListView
                            android:id="@+id/right_listView"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:divider="@android:color/transparent"
                            android:dividerHeight="0.1sp"
                            android:choiceMode="singleChoice"
                            >
                        </ListView>
                </LinearLayout>

Answer

Rohit Suthar picture Rohit Suthar · May 19, 2017

Here example for how to create GradientDrawable shape programmatically.

GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.RED);
shape.setStroke(3, Color.YELLOW);

For change the radius for all corners of the gradient.

shape.setCornerRadius(15);

For Change the radius for specific corners of the gradient.

shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });

You can use this drawable as a background as below :

view.setBackgroundDrawable(shape);