Progress Bar

We can display the progress bar dialog box to display the status of work being done like Downloading file, Loading page, etc.

In this example, we are displaying the progress dialog for dummy file download.

The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(), show() etc.

activity_main.xml

<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"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="62dp"
        android:layout_marginTop="44dp"
        android:text="@string/button" />
</RelativeLayout>


MainActivity

package com.example.progressbar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    TextView text;
    Button btn;
    int progressbarStatus = 0;
    ProgressDialog proDial;
    private Handler myHandle = new Handler();
    long fileSize = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        proDial = new ProgressDialog(v.getContext());
        proDial.setCancelable(true);
        proDial.setMessage("File Downloading...");
        proDial.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        proDial.setProgress(0);
        proDial.setMax(100);
        proDial.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (progressbarStatus < 100) {
                    progressbarStatus = doOperation();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }

                    myHandle.post(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            proDial.setProgress(progressbarStatus);

                        }
                    });
                }

                if (progressbarStatus >= 100) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                    proDial.dismiss();
                }
            }
        }).start();
    }

    public int doOperation() {
        while (fileSize <= 10000) {
            fileSize++;
            if (fileSize == 1000) {
                return 10;
            } else if (fileSize == 2000) {
                return 20;
            } else if (fileSize == 3000) {
                return 30;
            } else if (fileSize == 4000) {
                return 40;
            } else if (fileSize == 5000) {
                return 50;
            } else if (fileSize == 6000) {
                return 60;
            } else if (fileSize == 7000) {
                return 70;
            } else if (fileSize == 8000) {
                return 80;
            } else if (fileSize == 9000) {
                return 90;
            } else if (fileSize == 10000) {
                return 100;
            }
        }
        return 100;
    }
}


Output



Source Code: ProgressBar

Comments

Popular posts from this blog

SQLiteDatabase (Without SqliteOpenHelper class)

Set current date to Button and Datepicker dialog

MVC Architecture in Android