Search code examples
androidlayoutandroid-drawablecornerradius

How to set the same rounded corner to linear layouts?


I'm trying to make a single bar chart using Linear Layout (don't want to use MPAndroidChart or other libraries).
This is something i'd like to achieve:
enter image description here

enter image description here

So as you can see, the corners have the same degree/radius, no matter the width of the layout.
Here is what I did:
enter image description here

Which looks good, but here comes the problem: When having this Linear Layout with a smaller width, then the radius doesn't work the way I wanted
enter image description here

I would like to have the same radius angle on both, something like this (the blue part should but cut off):
enter image description here

I was hoping that I can set the radius property with some perecent, but this is not possible. Here is the layout:

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraint_layout_statistics_budgeting_recycler_view_chart"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginHorizontal="8dp"
            android:layout_marginTop="24dp"
            android:background="@drawable/rounded_bar_chart_total"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/image_view_statistics_budgeting_recycler_view_category_image">

            <LinearLayout
                android:id="@+id/linear_layout_statistics_budgeting_recycler_view_bar_chart"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/rounded_bar_chart_spent"
                android:orientation="horizontal"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

And the two drawable files (the only difference is the color):

<?xml version="1.0" encoding="utf-8"?>
<!-- rounded_bar_chart_total-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="48dp" />
    <padding
        android:bottom="@dimen/margin_extra_small"
        android:left="@dimen/margin_extra_small"
        android:right="@dimen/margin_extra_small"
        android:top="@dimen/margin_extra_small" />
    <solid android:color="#8FEFEEEC" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<!--rounded_bar_chart_spent-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="48dp" />
    <solid android:color="#5CE662" />
</shape>```

Any help how to do this?

Solution

  • You don't have to hardcode the progress bar by yourself. It seems that material components library implemented this (requires com.google.android.material:material:1.3.0'). Check here for github doc and here is an example I did:

        <com.google.android.material.progressindicator.LinearProgressIndicator
            android:id="@+id/linearProgressIndicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="false" // static progress bar
            android:progress="50"
            app:trackCornerRadius="10dp"
            app:trackThickness="20dp" />