articles

Home / DeveloperSection / Articles / Android Progress Bar

Android Progress Bar

Nitish Kesarwani 3290 28-Dec-2017

ProgressBar is a dialog box which gives the current status of Activity on Android such as downloading and uploading of files. Here, we are using ProgressDialog box, which is a sub-class of AlertDialog box which helps us to show the progress bar.

It has following method setCancelable(bool), SetMessage(string), setProgressStyle(style), setProgress(int) , setMax(int) and finally use show() to show the controls.

Attributes
android: indeterminate: It allows to determine the indeterminate mode
android: Max: We can set the maximum limit of a progress bar
 android: Progress: Defines the initial and final value of progress bar between 0 to 100
 android:progressDrawable: Uses a drawable object for the progress bar
 android:minorForRtl: When mirror mode for RTL(right to left) is enabled then drawable is associated
 android:secondaryProgress: Defines the secondary progress value between 0 to 100.
 android:animationResolution: To Switch between two frames in milliseconds
Methods 
getMin(): To get the minimum value of progress bar
setMin(): To set the minimum value of progress bar
 getMax(): To get the maximum value of progress bar
setMax(): To set the maximum value of progress bar
getProgress(): To get the update value of a progress bar
setProgress(): We have set initial and updated value of the progress bar
isAnimating(): It checks whether progress bar is animating or not
incrementProgressBy():Increase the progress bar value on specified value.
 drawableStateChanged(): Whenever the change of view state occurs, the impact is reflected in the drawable
 onAttachedToWindow(): It is called when view is attached to window
onSizeChanged(): It receives four integer parameters which describe the change of state in progress bar window
 Android_Manifest.xml
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.progressbar">

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


Main_Activity.xml
<LinearLayout

    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mapsample.msclient009.progressbar.MainActivity">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">
    <ProgressBar
        android:layout_marginTop="20dp"
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
       <Button
           android:id="@+id/btn"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Progress-Status" />
   </LinearLayout>
</LinearLayout>
Main_Activity.java
package com.mapsample.msclient009.progressbar;
import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
ProgressDialog pdbar;
Button btn;
int status = 0;
Handler progresHandler = new Handler ( );
long FSize = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
       progressChanged();
    }
    public void progressChanged()
    { btn =(Button)findViewById ( R.id.btn );
        btn.setOnClickListener ( new View.OnClickListener ( ) {
        @Override
        public void onClick(View v) {
            pdbar = new ProgressDialog ( v.getContext () );
            pdbar.setCancelable (true );
            pdbar.setMessage ("Downloading progress..." );
            pdbar.setProgressStyle (pdbar.STYLE_HORIZONTAL );
            pdbar.setProgress ( 0 );
            pdbar.setMax (100);
            pdbar.show ();
            status = 0;
            FSize = 0;
            new Thread ( new Runnable ( ) {
                @Override
                public void run() {
                while (status<100)
                {
                    status = checkStatus();
                    try{Thread.sleep ( 1000 );}catch (Exception e){e.printStackTrace ();}
                    progresHandler.post ( new Runnable ( ) {
                        @Override
                        public void run() {
                            pdbar.setProgress ( status );
                        }
                    } );
                }
                if(status>=100)
                { try{Thread.sleep ( 1000 );}catch(Exception e){e.printStackTrace ();}
                  pdbar.dismiss ();
                }
                }
            } ).start ();
          }
    } );
    }
    public int checkStatus() {
        while (FSize <= 5000) {
            FSize++;
            if (FSize == 500)
                return 15;
            else if (FSize == 1000)
                return 30;
            else if (FSize == 2000)
                return 45;
            else if (FSize==3000)
                return 100;
        }
        return 100;
    }
}

Launch Your APP

Android Progress Bar Android Progress Bar


Leave Comment

Comments

Liked By