Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular Progress Indicator ignores width/height attributes and doesn't scale

I need to fit 4 circular progress bars horizontally, like this:

enter image description here

so I decided to use new material component Circular Progress Indicator (https://material.io/components/progress-indicators/android). I spread chain these 4 progress bar in constrait layout, but them don't scale. It looks like they are still having deafult 24dp size. Even if I try to hardcode width and height to 200dp for example, the output is still small progress bar.

<com.google.android.material.progressindicator.CircularProgressIndicator
            android:id="@+id/progressBarCalories"
            style="@style/ProgressBarCircular"
            android:layout_marginTop="32dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="6dp"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintTop_toBottomOf="@id/wrapperCookingTime"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/progressBarProtein"
            tools:progress="75"/>

        <com.google.android.material.progressindicator.CircularProgressIndicator
            android:id="@+id/progressBarProtein"
            style="@style/ProgressBarCircular"
            android:layout_marginTop="32dp"
            android:layout_marginStart="6dp"
            android:layout_marginEnd="6dp"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintTop_toBottomOf="@id/wrapperCookingTime"
            app:layout_constraintStart_toEndOf="@id/progressBarCalories"
            app:layout_constraintEnd_toStartOf="@id/progressBarCarbs"
            tools:progress="75"/>

        <com.google.android.material.progressindicator.CircularProgressIndicator
            android:id="@+id/progressBarCarbs"
            style="@style/ProgressBarCircular"
            android:layout_marginTop="32dp"
            android:layout_marginStart="6dp"
            android:layout_marginEnd="6dp"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintTop_toBottomOf="@id/wrapperCookingTime"
            app:layout_constraintStart_toEndOf="@id/progressBarProtein"
            app:layout_constraintEnd_toStartOf="@id/progressBarFat"
            tools:progress="75"/>

        <com.google.android.material.progressindicator.CircularProgressIndicator
            android:id="@+id/progressBarFat"
            style="@style/ProgressBarCircular"
            android:layout_marginTop="32dp"
            android:layout_marginStart="6dp"
            android:layout_marginEnd="16dp"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintTop_toBottomOf="@id/wrapperCookingTime"
            app:layout_constraintStart_toEndOf="@id/progressBarCarbs"
            app:layout_constraintEnd_toEndOf="parent"
            tools:progress="75"/>

        <com.google.android.material.progressindicator.CircularProgressIndicator
            android:id="@+id/progressBar"
            style="@style/ProgressBarCircular"
            android:layout_marginTop="32dp"
            app:layout_constraintWidth_min="200dp"
            android:minWidth="200dp"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/progressBarFat"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:progress="75"/>

enter image description here

like image 365
Xengo Avatar asked Jan 24 '21 13:01

Xengo


3 Answers

You have to use the indicatorSize attribute:

   <com.google.android.material.progressindicator.CircularProgressIndicator
            android:indeterminate="true"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:indicatorSize="90dp"
            app:indicatorColor="@color/colorPrimary"
            ../>

enter image description here

like image 62
Gabriele Mariotti Avatar answered Oct 14 '22 08:10

Gabriele Mariotti


In version:

implementation 'com.google.android.material:material:1.5.0-alpha01'

You can set size using the following attribute:

app:indicatorSize="180dp"
app:trackThickness="10dp"

Full xml item will look something like:

<com.google.android.material.progressindicator.CircularProgressIndicator
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:indicatorSize="180dp"
    app:trackThickness="10dp"
    app:trackColor="@color/colorPrimary"
    app:indicatorColor="@color/colorPrimaryDark"
    android:layout_gravity="center_horizontal"
    android:progress="80"
    app:trackCornerRadius="8dp"
    android:layout_marginTop="30dp"/>

Please Note: You need to set XML Display Theme to "MeterialTheme" inorder to see UI updates.

like image 21
Hulisani Mudimeli Avatar answered Oct 14 '22 08:10

Hulisani Mudimeli


For those using data-binding, you can use

@BindingAdapter("bindProgressSizeToViewWidth")
fun bindProgressSizeToViewWidth(circularProgressIndicator: CircularProgressIndicator, view: View?) {
view ?: LogEventUtils.logApiError("bindProgressSizeToViewWidth: null view").run { return } // safty line you can remove it

    view.post {
        circularProgressIndicator.indicatorSize = view.measuredWidth
    }
}

XML (just give the text padding 12dp & center it within the circularProgressIndicator)

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/progressTv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif"
                    android:lineSpacingExtra="4sp"
                    android:padding="12dp"
                    tools:text="4/15"
                    android:textColor="@color/pumpkin_orange"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:layout_constraintStart_toStartOf="@id/circularProgressIndicator"
                    app:layout_constraintBottom_toBottomOf="@+id/circularProgressIndicator"
                    app:layout_constraintEnd_toEndOf="@+id/circularProgressIndicator"
                    app:layout_constraintTop_toTopOf="@+id/circularProgressIndicator" />

                <com.google.android.material.progressindicator.CircularProgressIndicator
                    android:id="@+id/circularProgressIndicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:indeterminate="false"
                    android:progress="@{40}"
                    app:bindProgressSizeToViewWidth="@{progressTv}"
                    app:indicatorColor="?colorPrimary"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:trackColor="@color/battleship_grey" />

Results

Results

like image 24
Dasser Basyouni Avatar answered Oct 14 '22 09:10

Dasser Basyouni