Search code examples
androidandroid-themeandroid-progressbarmaterial-components-androidmaterial-components

Error when trying to change the corner radius of Android ProgressBar


I have a ProgressBar widget with Widget.AppCompat.ProgressBar.Horizontal style:

<ProgressBar
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

When digging into Widget.AppCompat.ProgressBar.Horizontal (it uses Widget.Material internally) style definition in the AppCompat source codes, I've found that for API 21+ it uses a following drawable:

<style name="Widget.Material.ProgressBar.Horizontal" parent="Widget.ProgressBar.Horizontal">
    <item name="progressDrawable">@drawable/progress_horizontal_material</item>
    ...
</style>

Here is a fragment of progress_indeterminate_horizontal_material:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@id/background"
          android:gravity="center_vertical|fill_horizontal">
        <shape android:shape="rectangle"
               android:tint="?attr/colorProgressBackgroundNormal">
            <corners android:radius="?attr/progressBarCornerRadius" />
            <size android:height="@dimen/progress_bar_height_material" />
            <solid android:color="@color/white_disabled_material" />
        </shape>
    </item>

It uses ?attr/colorProgressBackgroundNormal attribute, however when I'm trying to set the value of this attribute in my application theme with the following line:

<item name="progressBarCornerRadius">2dp</item>

then I'm getting a compilation error:

AAPT: error: style attribute 'attr/progressBarCornerRadius (aka com.application:attr/progressBarCornerRadius)' not found.

Solution

  • The attribute name is android:progressBarCornerRadius but it is private.

    You can use a ProgressBar with a custom style:

    <style name="customProgressBar" parent="Widget.AppCompat.ProgressBar.Horizontal">
       <item name="progressDrawable">@drawable/custom_progress_horizontal</item>
    </style>
    

    Otherwise you can use the new LinearProgressIndicator provided by the Material Components Library:

        <com.google.android.material.progressindicator.LinearProgressIndicator             
            android:indeterminate="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:trackThickness="10dp"
            app:trackCornerRadius="8dp"/>
    

    Note: it requires at least the version 1.3.0-alpha04.

    enter image description here