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.
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 .
Dialogs should contain a maximum of two actions.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With