Android Studio: Set ProgressBar background color with RemoteView

65 views Asked by At

I have this progressbar view:

<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:layout_centerVertical="true"
        android:max="100"
        android:progress="20"
        android:progressDrawable="@drawable/progress_bar_shape"
        android:progressTint="#03ff46" // this is the value I'm trying to set via RemoteView
        android:progressBackgroundTint="#a8a8a8" // also this one
    />

I'm trying to set the background color & fill color with RemoteView (I'm using it for a widget)

views.setInt(R.id.progressBar, "setProgressTintList", clockData.getDarkColor());

My widget can't load, so this must be the wrong way to do this.

How can I fix this problem?

Thank you!

1

There are 1 answers

1
David Wasser On

You need to call setProgressBackgroundTintList() and setProgressTintList() on your remote Views. Both these methods take a ColorStateList as a parameter. So you need to call views.setColorStateList() to do this. See https://developer.android.com/reference/android/widget/RemoteViews?hl=en#setColorStateList(int,%20java.lang.String,%20android.content.res.ColorStateList)


ColorStateList is used to set a list of colors depending on the state of a UI element (pressed, enabled, selected, etc.). If you only want to set the color, regardless of its state, you can simply create a ColorStateList that contains a single color. Here's an example of creating a ColorStateList containing just a single color programatically and using that to set the color in RemoteViews:

ColorStateList csl = ColorStateList.valueOf(clockData.getDarkColor());
views.setColorStateList(R.id.progressBar, "setProgressTintList", csl);