In this article, we will see how we can add color to a ProgressBar in android. Android ProgressBar is a user interface control that indicates the progress of an operation. For example, downloading a file, uploading a file on the internet we can see the ProgressBar estimate the time remaining in operation. Note in this article we will be using Java and XML to set the color.
Step by Step Implementation
Step 1: Create a New Project
- To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
- Note that select Java as the programming language.
Step 2: Create a custom ProgressBar
- Go to the app > res > drawable > right-click > New > Drawable Resource File and name the file as progress_bg.
- Inside the XML file add a rotate tag with some attributes(see code)
- Inside rotate tag create a shape tag within which create the size and gradient tag
- Attributes of these tags are given in the code below.
- Below is the code for the progress_bg.xml file.
Step 3: Working with the activity_main.xml file
- Go to the activity_main.xml file and refer to the following code.
- Open the activity_main.xml file and in the ProgressBar tag and set the drawable in indeterminateDrawable attribute.
- Below is the code for the activity_main.xml file.
Step 4: Working with the MainActivity.java file
- Go to the MainActivity.java file and refer to the following code.
- Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.