Skip to content Skip to sidebar Skip to footer

Android Singleton Dialog

I have android application which deals with lot of progress dialogs.I have to create a separate dialog for each activity. Dialog creation takes a activity(context) as parameter whi

Solution 1:

Declare showProgressDialog and hideProgressDialog in Utill helper class as shown in following code snippet

publicstaticProgressDialogshowProgressDialog(Context context) {
        ProgressDialog pDialog = newProgressDialog(context);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        return pDialog;
    }

    publicstaticvoidhideProgressDialog(ProgressDialog pDialog) {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

Then call from activity where you need to show the ProgressDialog for example in onPreExecute() method of AsyncTask class as shown in below code snippet

ProgressDialogpDialog= Util.showProgressDialog(this);

and use following code to hide the progressDialog

 Util.hideProgressDialog(pDialog);

Solution 2:

Putting dialog code into a helper class's static method receiving a Context maybe the best way.

Solution 3:

Unfortunately, no. You have to attach the dialog to an activity, otherwise your application will tend to crash. You could get exceptions like android.view.WindowManager$BadTokenException for instance.

Solution 4:

It's not really the answer for your question, but maybe my idea helps you. I've created a BaseActivity, there is a member dialog, activity context as member and two methods, to show and to hide progess dialog. All other activities are extended from it.

Solution 5:

If you want ProgressBar only in the dialog define this class

publicclassProgressDialog {
    privateDialog dialog;
    privatestaticProgressDialog mInstance;

    publicstatic synchronized ProgressDialoggetInstance() {
        if (mInstance == null) {
            mInstance = newProgressDialog();
        }
        return mInstance;
    }

    publicvoidshow(Context context) {
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        dialog = newDialog(context,R.style.ProgressDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.layout_progress_dialog);
        dialog.setCancelable(false);
        dialog.show();
    }

    publicvoiddismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

and this XML in layout folder

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"><ProgressBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:progressDrawable="@drawable/progress"android:id="@+id/progress" /></LinearLayout>

and this style in style.xml

<stylename="ProgressDialog"parent="Animation.AppCompat.Dialog"><itemname="colorAccent">@color/colorPrimary</item><itemname="android:windowFrame">@null</item><itemname="android:windowBackground">@android:color/transparent</item><itemname="android:windowIsFloating">true</item><itemname="android:windowContentOverlay">@null</item><itemname="android:windowTitleStyle">@null</item><itemname="android:windowAnimationStyle">@android:style/Animation.Dialog</item><itemname="android:windowSoftInputMode">stateUnspecified|adjustPan</item><itemname="android:backgroundDimEnabled">true</item><itemname="android:background">@android:color/transparent</item></style>

Post a Comment for "Android Singleton Dialog"