Progress Dialog is closed when touch on screen
Asked Answered
N

4

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.

Nonfulfillment answered 29/10, 2013 at 10:33 Comment(2)
Did you solved your problem? How is going your dialog?Leishaleishmania
I used dialog.setCanceledOnTouchOutside(getRetainInstance()); these code and it works fine for me.Nonfulfillment
N
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)

Nonfulfillment answered 29/10, 2013 at 12:24 Comment(2)
how does this code will understand when data is fetched? and with what argument close the dialog?Bitt
in second thread you you process on your code. then you can close the dialog.Nonfulfillment
L
54

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.

Leishaleishmania answered 29/10, 2013 at 10:34 Comment(5)
i have used these yourDialog.setCanceledOnTouchOutside(false); and yourDialog.setCancelable(true); but it not worksNonfulfillment
try dialog.setCancelable(false); before Looper.loop();Egypt
@Nonfulfillment where do you put the line? I always put this line when i create my Dialog, not on run(){} function.Leishaleishmania
@Nonfulfillment 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().Leishaleishmania
spot on, solved my problem using AlertDialog. UPVOTED :)Markitamarkka
N
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)

Nonfulfillment answered 29/10, 2013 at 12:24 Comment(2)
how does this code will understand when data is fetched? and with what argument close the dialog?Bitt
in second thread you you process on your code. then you can close the dialog.Nonfulfillment
S
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.

Saccharate answered 29/10, 2013 at 10:48 Comment(0)
G
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();
Gothicism answered 8/4, 2014 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.