Android ProgressBar Example

Progress bar  is used to display the status of work . Like downloading file , uploading file etc. A progress bar can also be made indeterminate. In indeterminate mode, the progress bar shows a cyclic animation without an indication of progress. This mode is used by applications when the length of the task is unknown. The indeterminate progress bar can be either a spinning wheel or a horizontal bar.
The following example shows how a progress bar can be used from a worker thread to update the user interface to notify the user of progress:


Step 1: Create Project 
             a) Open Android Studio
             b) Go to File >New> New Project  > Project Name  > Next > Next > Next > Finish 

Step 2 : Layout Design 
              open activity_main.xml file and add the bellow code  

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ProgressBar Example "
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="30dp"
        android:layout_marginTop="25dp"
        android:gravity="center"/>

    <ProgressBar
        android:id="@+id/pb"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_margin="10dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/loading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="20dp"
        android:layout_below="@+id/pb"
        android:layout_marginTop="25dp"
        android:gravity="center"/>
</RelativeLayout>


Step 3. Now go to your MainActivity.java and add the following code to it.
 import android.os.Bundle;
 import android.os.Handler;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.View;
 import android.widget.ProgressBar;
 import android.widget.TextView;

 public class MainActivity extends AppCompatActivity {

    private TextView tvLoading;
    private ProgressBar mProgress;
    private int mProgressStatus = 0;

    private Handler mHandler = new Handler();
 
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          mProgress=(ProgressBar)findViewById(R.id.pb);
          tvLoading=(TextView)findViewById(R.id.loading);
          mProgress.setMax(100); // maximum value 
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < 100) {
                    mProgressStatus = mProgressStatus+5; // value increased by 5                     
                 try { 
                        Thread.sleep(1000);} 
                    catch (InterruptedException e) 
                    {
                      e.printStackTrace();
                     }
                    // Update the progress bar                     
 
                    mHandler.post(new Runnable() {
                        public void run() {
                           // set progress value
                          mProgress.setProgress(mProgressStatus);  
                          // set progress status
                        tvLoading.setText("Loading "+mProgressStatus+" %");                          
                       }
                    });
                }
            }
        }).start();
    }
  }
 
Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        Youtube : Android & Java Tutorial

 
 Step 4 : Output 


Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Coordinator Layout in Android