Home > Software engineering >  Change color Progress indicator with Databinding?
Change color Progress indicator with Databinding?

Time:11-24

I try all the ways but I can't change the color of progressbar. I tried to change it not only as binding but also in adapter but it doesn't work. Can you please help?

.....

    <com.google.android.material.progressindicator.CircularProgressIndicator
                            android:id="@ id/progressBar"
                            android:layout_width="160dp"
                            android:layout_height="160dp"
                            android:progress="25"
                            app:indicatorColor="@{session.getChannel() == Channel.MOBILE ? @color/cerulean : @color/teal}"
                            app:indicatorDirectionCircular="counterclockwise"
                            app:indicatorInset="7dp"
                            app:indicatorSize="140dp"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:trackColor="#DEE3ED"
                            app:trackCornerRadius="5dp"
                            app:trackThickness="9dp" />

i get this error in this case Cannot find a setter for <com.google.android.material.progressindicator.CircularProgressIndicator app:indicatorColor> that accepts parameter type 'android.content.res.ColorStateList' If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

val color = R.color.cerulean
             
progressBar.getIndeterminateDrawable().setColorFilter(color, 
                PorterDuff.Mode.SRC_IN)

And then I tried this code but again failed. undefined

CodePudding user response:

You can use custom binding adapter like this

@BindingAdapter("app:indicatorColor")
fun setIndicatorColor(progress: CircularProgressIndicator, color: Int) {
    progress.setIndicatorColor(color)
}

After that you can use your code in xml

app:indicatorColor="@{session.getChannel() == Channel.MOBILE ? @color/cerulean : @color/teal}"
  • Related