Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply rounded corner to a view based on its width in Android Kotlin

I am working on a custom Progress bar as photos below:

enter image description here

enter image description here

enter image description here

Basically, I created a drawable xml background file:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/jungleGreen"/>
    <corners
        android:bottomLeftRadius="40dp"
        android:topLeftRadius="40dp"/>
</shape>

Then I applied it to the view that I am using:

<View
            android:id="@+id/progress_bar_placeholder_view"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginEnd="45dp"
            android:background="@drawable/background_filled_patronage_progressbar"
            app:layout_constraintTop_toTopOf="parent"/>

It is totally fine and I can achieve scenario 1 and 2, but when the bar is getting closer to the end, how can I programmatically set the rounded corner for the top right and the bottom right part of the view until it looks like in photo 3?

Thanks.

like image 540
Long Dao Avatar asked Dec 15 '25 05:12

Long Dao


1 Answers

try 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);
        }

Source : How to create android shape background programmatically?

like image 81
Madhav Avatar answered Dec 16 '25 21:12

Madhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!