Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress Dialog is closed when touch on screen

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.

like image 558
Kailas Avatar asked Oct 29 '13 10:10

Kailas


People also ask

What is progress dialog in Android?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

How do I turn off progress dialog?

getText(R. string. msg_dlg_analyse_pic), true, //indeterminate true, new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { m_bRunning = false; } });

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.


2 Answers

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.

like image 121
JJ86 Avatar answered Sep 30 '22 14:09

JJ86


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)

like image 38
Kailas Avatar answered Sep 30 '22 13:09

Kailas