Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single alert dialog for entire application

I have to display many error messages and alert dialogues in my application. I do not want to use Toast, I prefer to use AlertDialog.

Instead of creating a new alert dialog in every activity, how do I create and maintain one alert dialog and just change the error message string in it?

Whatever activity I am in, I must be able to access the AlertDialog instance to show and dismiss it.

How can I achieve this? Kindly give me some lead on this.

like image 224
Achilles Avatar asked Nov 26 '12 09:11

Achilles


People also ask

What is the difference between an alert and an alert dialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How many action button can a dialog have?

Dialogs should contain a maximum of two actions.

How many buttons can an alert dialog display?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.


2 Answers

make one class and paste this function...(may be Utils.java)

public static void alertDialogShow(Context context, String message)
        {
            final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setMessage(message);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    alertDialog.dismiss();
              } 
            }); 
            alertDialog.show();
        }

and call this by writing..

Utils.alertDialogShow(YourActivity.this,"Your Error Message")
like image 103
Mehul Ranpara Avatar answered Sep 25 '22 11:09

Mehul Ranpara


You could always write a base class of Activity with your alertdialog call as a method and then for any of your activity classes instead of using extends Activity, use extends MyBaseActivity and then call the method whenever you need it by passing the string you want output.

like image 27
David Hirst Avatar answered Sep 24 '22 11:09

David Hirst