I am using a determinate circular progress indicator in my Fragment. Depending on the progress, the progress indicator shall have different colors. So, if the progress is 100%, it shall turn red, otherwise it is blue.
In a previous project I successfully used the following code, which is not working anymore - and I cannot find the reason for it.
The code was originally taken from this post: Get color value programmatically when it's a reference (theme)
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getContext().getTheme();
@ColorInt int color;
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
color = typedValue.data;
circularProgressIndicator.setIndicatorColor(color);
The error already pops up during building.
The R class I am using is the right one --> import mypackage.R;
The theme is set properly (or even too often!?)
- in the Manifest file
- in the Main Activity Layout XML
- in the Main Activity before "setContentView)
- in the Fragment Layout XML
Does anybody have an idea why I cannot select the color from my theme this way?
I cannot set the the color directly from R.color as it depends on day/night mode. An other approach would be to set different styles for the progress indicator in code, but it seems that the class CircularProgressIndicator is not offering this as for example a TextView does.
Colors XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ReminderBlue">#00337A</color>
<color name="ReminderError">#B00020</color>
...
</resources>
Theme.XML
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.Reminder" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/ReminderBlue</item>
<item name="colorError">@color/ReminderError</item>
.....
</style>
FragmentLayout.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:theme="@style/Theme.Reminder"
tools:context=".activities.fragments.MainFragment">