16

I use a ProgressDialog in the thread. In the onButtonClick the thread is started, but when I touch anywhere on the screen the ProgressDialog is closed.

How can I prevent this?

private void ButtonClick(View view) {

    btn1 = (Button) view.findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            GetStudentData();
        }
    });
}

private synchronized void GetStudentData() {  
    try {
        // Thread to display loader
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                dialog = Msg.ShowProgressDialogBox(
                    getActivity(),
                    dialog,
                    "Please wait while we fetch the student data...");
                Looper.loop();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                String studentData="Kailas";
            }   
        }
    } catch(Exception ex {
    }
}

Update

When I touch on the screen the ProgressDialog disappears but get all data.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Kailas
  • 3,173
  • 5
  • 42
  • 52

4 Answers4

55

Add this to your dialog:

yourDialog.setCanceledOnTouchOutside(false);

In this way you can touch screen and the Dialog will be not canceled.

EDIT

If you are using DialogFragment, then you must use the following code snippet before calling show(FragmentManager mng, String tag):

More info here.

dialogFragment.setCancelable(false);

EDIT 2

Be careful with ProgressDialog , because with Android Oreo (v8.0) it is now deprecated.

JJ86
  • 5,055
  • 2
  • 35
  • 64
  • i have used these yourDialog.setCanceledOnTouchOutside(false); and yourDialog.setCancelable(true); but it not works – Kailas Oct 29 '13 at 10:41
  • try dialog.setCancelable(false); before Looper.loop(); – Adrien Cerdan Oct 29 '13 at 10:46
  • @Kailas where do you put the line? I always put this line when i create my Dialog, not on run(){} function. – JJ86 Oct 29 '13 at 10:46
  • @Kailas there is something wrong elsewhere; i would rather use an AsyncTask, where you can create a Dialog and also show() and dismiss() it with onPreExecute() and onPostExecute(). – JJ86 Oct 29 '13 at 11:10
  • spot on, solved my problem using `AlertDialog`. UPVOTED :) – tony gil Nov 10 '16 at 09:34
5
private synchronized void GetStudentData() {

    try {
    // Thread to display loader
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            dialog = Msg.ShowProgressDialogBox(getActivity(),
                    dialog,
                    "Please wait while we fetch the student data...");
            dialog.setCanceledOnTouchOutside(getRetainInstance());
            Looper.loop();
        }
    }.start();

    new Thread() {

        @Override
        public void run() {
                  String studentData="Kailas";
         }   
    }
}

I Have used this line dialog.setCanceledOnTouchOutside(getRetainInstance()); and it worked fine in my android device OS(4.0.1) also tested on OS(2.6.3)

Kailas
  • 3,173
  • 5
  • 42
  • 52
  • how does this code will understand when data is fetched? and with what argument close the dialog? – hadi Apr 29 '15 at 03:38
  • 1
    in second thread you you process on your code. then you can close the dialog. – Kailas Apr 29 '15 at 05:11
3

When you are making certain important data downloading process then you can make progress dialog not able to cancel using below code:

mDialog.setCancelable(false);
mDialog.setCanceledOnTouchOutside(false);
mDialog.setOnCancelListener(new Dialog.OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
        // DO SOME STUFF HERE
    }
}

Also you can go for the progress bar in action bar if you are using in your project.

Hope it will help you.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
2

Make a class like this:

public class Progresss {

    static ProgressDialog dialog;

    public static void start(Context context) {
        dialog = new ProgressDialog(context);
        try {
            dialog.show();
        } catch (BadTokenException e) {
        }
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.progressdialog123);
        // dialog.setMessage(Message);
        // return dialog;
    }

    public static void stop() {
        if(dialog!=null)
            dialog.dismiss();
    }
}

This is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

Usage is like this:

Progresss.start(context);
Progresss.stop();
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Ali
  • 1,857
  • 24
  • 25