Android Horizontal ProgressBar Tutorial With Example

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

Output

Tutorialwing Android Horizontal ProgressBar Output Android Horizontal Progressbar Widget tutorial with example

Tutorialwing Android Horizontal ProgressBar Output

Getting Started

Android ProgressBar widget can be defined as below –

Android progressBar widget is an user interface element that represents progress of an operation.

Horizontal progressBar has horizontal bar to show progress of an operation. You may have determinate / indeterminate mode for horizontal progressBar.

Attributes of Android horizontal progressbar Widget

We have already covered the attributes of android progressBar in ProgressBar Tutorial With Example tutorial. So, you may visit post to know more.




Example of Android Horizontal ProgressBar Widget

Now, we will create an android application. Then, we will use horizontal progressBar in this application.

1. Creating New Project

Follow the steps below to create new application. Pleas ignore the steps if you have already created an application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ProgressBarHorizontal. 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 horizontal progressBar.

2. Modify Values folder

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

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

3. Use Horizontal 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/progressBarStyleHorizontal"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:indeterminate="true"
		android:layout_margin="20dp"/>

	<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 used progressbar and button widget. Now, we will access this widget in java file.
Note the style(“?android:attr/progressBarStyleHorizontal”) used in progressbar widget. This style is responsible for displaying progressbar in horizontal mode.

4. Access Horizontal ProgressBar Widget in java file

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

package com.tutorialwing.progressbarhorizontal;

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 file, we have accessed horizontal progressBar and button. Then, we have set click listener to show/hide progressbar when button is clicked.

Since, AndroidManifest.xml file is very important in any android application. So, we will also go through the content 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.progressbarhorizontal"
		  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 the output as shown above.

That’s end of tutorial on Android Horizontal ProgressBar widget.

Leave a Reply