Thursday, July 30, 2015

andrpod: Using ProgressDialog in Android Activity

In situations when need to wait some operation it is good practice to notify user that operation is in progress.
For this cases in Android present several classes which can help with this. One of them I am going to demonstrate.
I will show how to use ProgressDialog class for showing progress dialog. I will show how to create preogress dialog with title and without title.
There are several show methods.
I will take this one:
ProgressDialog.show(Context context, CharSequence title, CharSequence message);
In my case I write
ProgressDialog.show(Main.this"In progress""Loading");
Now my progress dialog shows on activity
with_title
Okay. It is working.
What if I want to hide title? It is also very easy, just put empty string as title parameter. Example:
ProgressDialog.show(Main.this"""Loading...");
It is working very good.
without_title
Need to stop it:
progressDialog.dismiss();


Full code, for testing my words:


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

/**
 * Class for showing how to work with progress dialog
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {

    private Button clickBtn;
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickBtn = (Button) findViewById(R.id.click);
        clickBtn.setText("Click me");
        clickBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //start the progress dialog
                progressDialog = ProgressDialog.show(Main.this, "", "Loading...");
                new Thread() {
                    public void run() {
                        try {
                            sleep(10000);
                        } catch (Exception e) {
                            Log.e("tag", e.getMessage());
                        }
                        // dismiss the progress dialog
                        progressDialog.dismiss();
                    }
                }.start();
            }
        });
    }
}





Progress dialog is one of UI classes which helps to notify user about long operations.

No comments:

Post a Comment