
22 May 2024 Leave a comment Tech-Help
Customizing the appearance of UI elements is a common task in Android development. One such customization involves changing the color of a ProgressBar. In this guide, we will explore various methods to change the progress color of a horizontal ProgressBar programmatically.
Method 1: Using ColorFilter
One of the most straightforward methods to change the color of a horizontal ProgressBar is by using a ColorFilter
. This method can be applied to both indeterminate and determinate progress bars.
For Determinate ProgressBar
progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
Note: This changes the appearance of all progress bars in your application. To modify only a specific ProgressBar, you can use the following code:
Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);
For Indeterminate ProgressBar
If your ProgressBar is indeterminate, use the getIndeterminateDrawable()
method instead:
progressBar.getIndeterminateDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
Method 2: Using Progress Tint (API 21+)
Starting from Android Lollipop (API 21), you can set a progress tint directly:
progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
Method 3: Using XML Attributes
For those who prefer XML configurations, you can define the color directly in your layout file:
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/secondary" />
This solution works for API level 21 and higher.
Additional Resources
For more advanced configurations and detailed explanations, you can refer to our documentation:
Streamline Your Mobile App Testing with Repeato
While customizing your ProgressBar is an essential part of creating a polished app, ensuring that your app functions correctly is even more critical. This is where Repeato comes in. Repeato is a no-code test automation tool that allows you to create, run, and maintain automated tests for your iOS and Android applications quickly and efficiently.
Repeato leverages computer vision and AI, making it particularly fast to edit and run tests. It enables developers to focus on creating a great product instead of spending time on testing. Additionally, Repeato allows non-technical colleagues or QAs to handle test automation, further streamlining the development process.
Learn more about how Repeato can enhance your app testing workflow by visiting our documentation and blog.
Like this article? there’s more where that came from!
- Resolving the “xcrun: error: invalid active developer path” Error on macOS
- Adding Existing Frameworks in Xcode 4: A Comprehensive Guide
- Disabling ARC for a Single File in Xcode: A Step-by-Step Guide
- Resolving the Xcode-Select Active Developer Directory Error
- Resolving the “Multiple Commands Produce” Error in Xcode 10