0

How to center progress indicator in ProgressDialog easily (when no title/text passed along)

trying to create a custom progressdialog, but not working..

my code:

public class CustomProgressDialog extends ProgressDialog {

public CustomProgressDialog(Context context) {
        super(context, R.style.progress_dialog);
}

public static CustomProgressDialog show(Context context, CharSequence title,
        CharSequence message) {
    return show(context, title, message, false);
}

public static CustomProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate) {
    return show(context, title, message, indeterminate, false, null);
}

public static CustomProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate, boolean cancelable) {
    return show(context, title, message, indeterminate, cancelable, null);
}

public static CustomProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate,
        boolean cancelable, OnCancelListener cancelListener) {
    CustomProgressDialog dialog = new CustomProgressDialog(context);
    dialog.setTitle(title);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(cancelListener);
    dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    dialog.show();

    return dialog;
}
}

and how i d like to call:

public static CustomProgressDialog dialog;

public static void showLoaderDialog(String sHead, String sMess) {
    dialog=new CustomProgressDialog(mycontext).show(mycontext, sHead, sMess, true, true);
}
public static void hideLoaderDialog() {
    dialog.dismiss();
}

but i get only crash:( why? how can call this custom dialog?

error log:

04-05 20:33:12.754: ERROR/AndroidRuntime(13165): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:174)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.android.internal.app.AlertController.installContent(AlertController.java:201)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.app.AlertDialog.onCreate(AlertDialog.java:260)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.app.ProgressDialog.onCreate(ProgressDialog.java:176)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.app.Dialog.show(Dialog.java:225)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.lacas.chathuProto.CustomProgressDialog.show(CustomProgressDialog.java:37)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.lacas.chathuProto.CustomProgressDialog.show(CustomProgressDialog.java:26)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.lacas.chathuProto.chathuStart.showLoaderDialog(chathuStart.java:814)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.lacas.chathuProto.chathuPROTO$12.run(chathuPROTO.java:1159)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.os.Handler.handleCallback(Handler.java:587)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.os.Looper.loop(Looper.java:123)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at java.lang.reflect.Method.invoke(Method.java:521)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):     at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
lacas
  • 13,167
  • 28
  • 102
  • 175

1 Answers1

1

Maybe, you should call dialog in this way:

public static void showLoaderDialog(String sHead, String sMess) {
  dialog=new CustomProgressDialog(mycontext);
  dialog.show(mycontext, sHead, sMess, true, true);
}

But I can be wrong.

Anyway, show, please, wich code line refers to CustomProgressDialog.java:37

Anton Derevyanko
  • 3,241
  • 1
  • 22
  • 32