Progress Dialog simple sample source code

Progress Dialog In Android

ProgressDialog is a  dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.The dialog can be made cancelable on back key press.The progress range is 0..10000.

The ProgressDialog is used to show Loading action when performing the  background tasks like reading file , performing a http connection etc. This is very much coupled with the AsyncTask.

Progress Dialog Usage

The ProgressDialog provides 2 types of constructors, which takes context as one of its paramenter.
ProgressDialog(Context context)
ProgressDialog(Context context,int theme)

Note: when working with fragments use getActivity() method to get the activity context.
Official Documentation : Progress Dialog
 

Progress Dialog Sample Source Code


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

public class MainActivity extends Activity {

    ProgressDialog barProgressDialog;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // create a Progress Dialog Object
        barProgressDialog = new ProgressDialog(MainActivity.this);

        // Set variuous attributes of the ProgressDialog object
        barProgressDialog.setTitle("Loading ...");
        barProgressDialog.setMessage("Please wait ( about 4 sec)");
        barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(20);
        barProgressDialog.show();

        // This is used to dismiss the dialog, This can be invoked when ever yopu want the dialog to disappear, like on button click

        barProgressDialog.dismiss();
       
    }

}



Proudly powered by Sacnorth Media