Progress Dialog is closed when touch on screen

为君一笑 提交于 2019-11-30 11:27:59

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.

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)

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.

Ali

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!