Android ProgressBar Tutorial With Example

Hello Readers! In this post, we are going to learn about android progressbar widget. We will also go through different attributes of android progressbar that can be used to customise progressBar widget.

Output

Tutorialwing Android ProgressBar Output Android Progressbar widget tutorial example

Tutorialwing Android ProgressBar Output

Getting Started

Android progressbar can be defined as below –

A progressBar is a view that represents progress of an operation.

ProgressBar can be used in two ways depending on the types of operation. If you know the duration of an operation, you should use determinate progressBar. Determinate progressBar works only for a limited period of time. Or, If you do not know the duration of an operation, you should use indeterminate progressBar. Indeterminate progressBar works until you stop it.

Attributes of Android ProgressBar Widget

Now, we will go through different attributes of progressbar widget that can be used to customise this widget.

Sr. XML Attributes Description
1 android:indeterminate Enables indeterminate mode of the progressbar
2 android:indeterminateDrawable Sets drawable of the progressbar for the indeterminate mode
3 android:indeterminateDuration Sets duration of the indeterminate animation
4 android:max Sets maximum value
5 android:min Sets minimum value
6 android:progress Defines default progress value (between 0 and max value)
7 android:progressDrawable Drawable used for the progress mode
8 android:secondaryProgress Defines secondary progress value (between 0 and max value)

Some of the popular attributes of ProgressBar inherited from View are –

Sr. XML Attributes Description
1 android:alpha Sets alpha of view
2 android:background Sets background drawable for the view.
3 android:clickable Specifies if this view is clickable or not.
4 android:elevation Specifies base z-depth of the view.
5 android:id Specifies id of the view. Id must be unique.
6 android:onClick Specifies action to perform when this view is clicked.
7 android:padding Specifies padding of the view.
8 android:visibility Specifies visibility of the view. For example, VISIBLE, INVISIBLE etc.



Example of Android ProgressBar Widget

At First, we will create android project. Then, we will use progressBar widget in this project.

1. Creating New Project

Follow the steps below to create new project. Please ignore the steps if you have already created a new project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ProgressBar. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use progressBar widget in android.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">ProgressBar</string>
</resources>

3. Use ProgressBar Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<ProgressBar
		android:id="@+id/progressBar"
		style="?android:attr/progressBarStyle"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="Hide Progressbar"/>

</LinearLayout>

In activity_main.xml file, we have added progressbar and button widget in xml file. Now, we will access this widget in MainActivity.java file. Then, we will show/hide progressbar widget on button click.

4. Access ProgressBar Widget in java file

Open src/main/java/com.tutorialwing.progressbar/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.progressbar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ProgressBar progressBar = findViewById(R.id.progressBar);
		if (progressBar != null) {
			final Button btn = findViewById(R.id.button);
			if (btn != null) {
				btn.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int visibility = (progressBar.getVisibility() == View.GONE) ? View.VISIBLE : View.GONE;
						progressBar.setVisibility(visibility);

						String btnText = (progressBar.getVisibility() == View.GONE) ? "SHOW PROGRESSBAR" : "HIDE PROGRESSBAR";
						btn.setText(btnText);
					}
				});
			}
		}
	}
}

In MainActivity.java, we have accessed progressbar and button widget defined in activity_main.xml file. Then, we have added click listener on button to show/hide progressbar.

Since AndroidManifest.xml file is very important in any android application, we will also go through code inside this file.

AndroidManifest.xml

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.progressbar"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the application, we will get output as shown above.

That’s end of tutorial on Android ProgressBar Widget.

Leave a Reply