How to set TopLeftRadius and BottomLeftRadius for a GradientDrawable programmatically in Android
Asked Answered
T

3

7

I have a ShapeDrawable . I want to set the CornerRadius (TopLeftRadius & BottomLeftRadius) programmatically.

Below is my xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:top="5dp"
    android:bottom="5dp">

    <shape android:shape="rectangle">
        <corners android:topLeftRadius="20dp"
            android:bottomLeftRadius="20dp"/>

        <solid android:color="#A2D368"/>
    </shape>
</item>
</layer-list>
Tonisha answered 21/2, 2017 at 12:43 Comment(0)
M
16

Do like this;

    float mRadius=3f;

    GradientDrawable drawable=new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setCornerRadii(new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius});

for more details click here

Malathion answered 21/2, 2017 at 12:49 Comment(0)
T
5

Use setCornerRadii (float[] radii) method of GradientDrawable.

In general this method takes a float array of size 8.

For each of the 4 corners 2 radii values are needed x radii and y radii.

The corners are ordered top-left, top-right, bottom-right, bottom-left in float array.

Since you asked for topleft and bottomleft radius, you need to set radii value to first 2 and last 2 indexes of float array.

sample -> setCornerRadii(new float[]{50, 50, 0, 0, 0, 0, 50, 50})

Transliterate answered 15/6, 2021 at 6:53 Comment(1)
For a person who didn't use Radii before, it's a good descriptive answer. To know which value represents what side. Thumbs up.Lotti
B
0
<shape android:shape="rectangle">
    <corners
        android:topLeftRadius="@dimen/button_radius"
        android:topRightRadius="0dp"
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="@dimen/button_radius"/>
    <stroke
        android:width="@dimen/button_stroke_width"
        android:color="@color/colorBlack"/>
    <gradient
        android:angle="-90"
        android:startColor="@color/colorLightPurple"
        android:endColor="@color/colorDeepPurple" />
    </shape>
Bobette answered 21/2, 2017 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.