I'm making an Android app with some ProgressBars. The ProgressBar background must always be white, while the progress color must change based on a variable, for example:
This is the XML file of the ProgressBar style:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="ring" android:thicknessRatio="16" android:useLevel="false"> <solid android:color="#FFFF"/> </shape> </item> <item android:id="@android:id/progress"> <shape android:shape="ring" android:thicknessRatio="16" android:useLevel="true"> <solid android:color="@color/white" /> </shape> </item> </layer-list>
The progress color is set to white as the background. How could i change it?
Set up your progress bar in xml:
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/nameofyourcustomprogressbar"
When you access the spinner you can set the progress color property:
ProgressBar spinner = (ProgressBar) findViewById(R.id.progressBar1);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#bb86fc"), PorterDuff.Mode.MULTIPLY);
The #bb86fc is a color in a hex string format.
If you had listed a color in values/colors.xml and wanted to access a color that way:
<resources>
<color name="purple_200">#FFBB86FC</color>
</resources>
Access the color you want from values/colors.xml :
Color.parseColor(getString(R.color.purple_200));
If you get an error underline - "expected resource of type string" just add the annotation
@SuppressLint("ResourceType")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.