How to change progress bar progress color in Android?


What is the Progress Bar in Android?

Progress Bar is a widget in android which is used to indicate the loading screen within an android application. It is generally displayed when the data is being loaded within an android application from a server. In this article we will take a look on How to change progress bar color in Android.

Implementation

We will be creating a simple application in which we will be simply displaying a text view for displaying the heading of our application and we will be displaying a progress bar. We will be changing the color of that progress bar and adding a custom color to it.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:padding="4dp"
      android:text="Progress Bar in Android"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="20sp"
      android:textStyle="bold" />

   <!-- creating a progress bar on below line and we are adding a custom color for our progress bar in the parameter named as indeterminateTint -->
   <ProgressBar
      android:id="@+id/progressBar"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVHeading"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:indeterminate="true"
      android:indeterminateTint="#036E07" />
</RelativeLayout>

Explanation − In the above code we are creating a root layout as Relative layout. Inside this relative layout we are creating a simple text view which is used to display the heading within our application. We will be displaying a message inside this text view as a heading for our application. After creating this text view, we are creating a progress bar inside the progress bar, we are firstly specifying the id for it.

Then we are adding weight and height for the progress bar as wrap_content. Then we are aligning our progress bar to below of the heading using the id. After that we are adding center horizontal and center vertical for our progress bar to true to align it centrally. Then we are adding indeterminate property to true for progressbar. Lastly we are adding a color for our progress bar. After that we are adding a closing tag for our Relative Layout.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above tutorial we have taken a look at How to create a progress bar in android application and how we can add a custom color for our progress bar in android application.

Updated on: 30-Mar-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements