Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put Progress dialog in seperate class and call in every activity in android?

Tags:

android

I have a progress dialog in my every activity and in every activity I write code for progress dialog with different message where I want.Is there any way to put progress dialog code in seperate class and call that class in activity where I want to show that progress dialog.

here is my code for progress dialog:-

ProgressDialog m_Dialog = new ProgressDialog(CLoginScreen.this);
    m_Dialog.setMessage("Please wait while logging...");
    m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    m_Dialog.setCancelable(false);
    m_Dialog.show();
like image 869
Raghvender Kumar Avatar asked May 25 '16 05:05

Raghvender Kumar


1 Answers

You can define a class to encapsulate this operation and maybe some other involving dialogs. I use a class with static methods, something like this:

public class DialogsUtils {
    public static ProgressDialog showProgressDialog(Context context, String message){
        ProgressDialog m_Dialog = new ProgressDialog(context);
            m_Dialog.setMessage(message);
            m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            m_Dialog.setCancelable(false);
            m_Dialog.show();
            return m_Dialog;
        }

} 

In the Activity class:

ProgressDialog myDialog= DialogUtils.showProgressDialog(this,"some message");
...
myDialog.dismiss();

Of course you can add others parameters to the operation so it can be more flexible.

Hope it helps.

like image 56
Pablo Avatar answered Oct 19 '22 10:10

Pablo