Home » Android

Progress Bar in Android Application

In this article, we are going to implement a progress bar to show process running in background in Android.
Submitted by Manu Jemini, on February 16, 2018

In the example below, we are going to use the App.ProgressDialog class to show the progress bar on the screen. We take advantage of the object of ProgressBar class by attaching our context like this: ProgressDialog(this).

To implement this in your program, you will need to import: android.app.ProgressDialog, android.widget.ProgressBar in your java file.

Then create a layout file with a single Button like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click here to see the progress bar"
	android:onClick="click"
	android:id="@+id/button2"
	android:layout_marginLeft="30dp"
	android:layout_alignParentTop="true"
	android:layout_centerHorizontal="true"
	android:layout_marginRight="30dp" />

The Button in the layout file is assigned to a method in the JAVA file, the function is click(). We assign on click function directly like this : android:onClick="click".

Then, we will make a new object of progressBar and assign it with the multiple properties like title, style and also tell the current progress, which in this case is set to 0.

After this we create a new thread and in every 500ms increase the amount of progress completed by this: P.setProgress(JT)

1) Java file:

package com.example.hp.myapplication;

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

public class MainActivity extends AppCompatActivity
{
	ProgressDialog P;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void click(View view){
		P=new ProgressDialog(this);
		P.setMessage("Progress bar running");
		P.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		P.setIndeterminate(true);
		P.setProgress(0);
		P.show();

		final int T = 50;
		Thread thread = new Thread() {
			@Override
			public void run() {
				int JT = 0;
				while(JT < T) {
					try {
						sleep(500);
						JT += 3;
						P.setProgress(JT);
					}
					catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		thread.start();
	}
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to see the progress bar"
        android:onClick="click"
        android:id="@+id/button2"
        android:layout_marginLeft="30dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginRight="30dp" />


</RelativeLayout>

3) Manifest file:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Output

progress bar 0
progress bar 1







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.