Achilles Achilles - 6 months ago 14
Android Question

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 but i prefer to use ALertDialog.

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

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

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

Answer

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")
Comments