» Java » How to implement Linear ProgressBar in any android studio project

How to implement Linear ProgressBar in any android studio project

Thumbnail of How to implement Linear ProgressBar in any android studio project

Progress bars are used to show progress of a task. For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.

In android there is a class called ProgressDialog that allows you to create progress bar. In order to do this, you need to instantiate an object of this class. The code is below.

activity_main.xml


    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


            android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp"
        android:text="Progress bar"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="30dp"
        android:textStyle="bold" />


            android:id="@+id/button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@android:color/holo_blue_bright"
        android:onClick="download"
        android:text="Click Here" />


 

MainActivity.java

 

package com.friendvilla.simplecode;

import android.app.Activity;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

    Button b1;
    private ProgressDialog progress;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button);
    }

    public void download(View view){
        progress=new ProgressDialog(this);
        progress.setMessage("Progress Bar Start");
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.setIndeterminate(true);
        progress.setProgress(0);
        progress.show();

    final int totalProgressTime = 100;
        final Thread t = new Thread() {
            @Override
            public void run() {
                int jumpTime = 0;

                while(jumpTime < totalProgressTime) {
                    try {
                        sleep(200);
                        jumpTime += 10;
                        progress.setProgress(jumpTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t.start();
    }

}
 

 

 

 


If this project's source code is password protected, its password can be found in the YouTube video.

Tutorial/Demo Video: not Available

Get The Source Code Now

This file has been licensed by MakeEasy - (reselling strictly prohibited)

Related Projects

Canvas is not supported in your browser.