Don Chakkappan Don Chakkappan - 21 days ago 10
Java Question

Android custom Progress Dialog - setCancelable()

I had created a custom

Progress Dialogue
. Overriding properties like
setCancelable()
,
setCanceledOnTouchOutside()
doesn't worked for me.

public class CustomProgressDialogue extends ProgressDialog {

private final Context context;

public CustomProgressDialogue(Context context) {
super(context);
this.context = context;
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_view);

}

// Didn't worked
@Override
public void setCancelable(boolean flag) {
super.setCancelable(false);
}
// Didn't worked
@Override
public void setCanceledOnTouchOutside(boolean cancel) {
super.setCanceledOnTouchOutside(false);
}
}


But the same time applying same properties after the instance creation worked.

// Worked
progressDialogue = new CustomProgressDialogue(getContext());
progressDialogue.setCancelable(false);
progressDialogue.setCanceledOnTouchOutside(false);


Could you please somebody can explain this?

Answer Source

instead of overriding the cancel-able method create a static method like this and then pass it the required options

here's how ProgressDialog class do this:

 public static ProgressDialog show(Context context, CharSequence title,
            CharSequence message, boolean indeterminate,
            boolean cancelable, OnCancelListener cancelListener) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setIndeterminate(indeterminate);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show();
        return dialog;
    }