I have created a Utils class, which consists of progressDialog and AlertDialog:

public class Utils {

    public static ProgressDialog dialog;
    private static AlertDialog.Builder builder;
    private static AlertDialog alert;

    public static void showLoader(Activity activity) {
        if (dialog == null) {
            dialog = new ProgressDialog(activity);


    public static void hideLoader() {
        if (dialog != null && dialog.isShowing()) {
            dialog = null;

    public static void showOKMessageandBack(String message, final Context activity, final Class activityClass) {
        builder = new AlertDialog.Builder(activity);
        if (message.length() <= 0)
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(activity, activityClass);
        alert = builder.create();

    public static void showAlertDialog(final Context context , String title , String message  ){

        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // setting Dialog title

        // setting Dialog message

        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {


  • \$\begingroup\$ You cannot ask us to add code to your class, though we can review it :) I edited that part out of your question and corrected some of the spelling mistakes it had. \$\endgroup\$
    – IEatBagels
    Commented Oct 29, 2015 at 15:25
  • \$\begingroup\$ thank you for advice , I edit my question , I need review this class , is their any way to make looks code better \$\endgroup\$
    – Mina Fawzy
    Commented Oct 29, 2015 at 15:28

2 Answers 2


I'm looking at this class and I'm confused.
The reason why is that I'm doing web programming so a dialog can be asked on different sessions.
In that case this class is useless.

In stead of holding your ProgressDialog and AlertDialog in your class as static variable, just return them in the method.
Then your

public static void hideLoader()

should be changed to :

public static void hideLoader(ProgressDialog dialog)

So with a small refactoring you create a class what could be good for multiple users application.
Other advantage is you can call the method multiple times, where in your setup this was impossible and even dangereous.

Naming your class :

A class Utils is just so poor chosen.
If you see a class name like that, what do you expect what's in it?
It could be Utils for Strings like StringUtils or more like MathUtils?
As you see at this name, you already know what to expect from it.
A better name could be DialogUtil

Constants :

In android there is a class R.string who have a lot of constants.
In stead of doing this :

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

You could do :

builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

It's cleaner code who saves a little amount of memory because you don't need to recreate the String every time you call the method.


Static variables

The variables builder and alert should not be static, as they are only used in one function and do not persist.

The one place you use alert past the end of the function is in the callback function. But you shouldn't be using it there:


should be:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.