Wednesday, August 31, 2011

MessageBox for Android

I switched to android from C# world. I have a tendency to program it like C# as I think it is fairly more readable than the way they use braces in Java. Here is a static class which you can easily use in you application to display a message box. I just tried to follow the simple MessageBox syntax we have in C#. You may need to pass the "this" pointer of your Activity . (Activity is kind of an Android version of Form or Dialog)

class EBMessageBox
{

static void ShowMessage(String text, Activity parent)
{
final AlertDialog alertDialog = new AlertDialog.Builder(parent)
.create();
alertDialog.setTitle("Wran");
alertDialog.setMessage(text);

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

alertDialog.cancel();

}
});
alertDialog.show();
}

}

No comments: