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.
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.
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();