Translate this page to

Search Android

Search For Android Tutorials (NEW, Custom Search for Android Developers)
Loading

Tuesday, January 13, 2009

Displaying AlertDialog in Android

After 3 days of not posting and a whole day staring at the command line, here we are again with Android. This time its how to display AlertDialog in Android. I'll just post the core codes and you can download the source code to see the whole code
public void onClick(View view) {
  if(view == this.alertBtn){
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Alert 1");
    alertDialog.setMessage("This is an alert");
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
    } });
  }else if(view == this.alert2Btn){
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Alert 2");
    alertDialog.setMessage("This is another alert");
    alertDialog.setIcon(R.drawable.search);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
    } });
    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
    }});
  }else if(view == this.alert3Btn){
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Alert 3");
    alertDialog.setMessage("This is the third alert");
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
    } });
    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
    }});
    alertDialog.setButton3("Middle", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        return;
    }});
  }
  alertDialog.show();
}



Quick Explanation
alertDialog = new AlertDialog.Builder(this).create();
- Here using the AlertDialog Builder, we shall build a new AlertDialog on the current activity;

alertDialog.setTitle("Alert 1");
alertDialog.setMessage("This is an alert");

- This line is logical, it sets the title and message of our AlertDialog

alertDialog.setButton<[ ]|2|3>("OK", new DialogInterface.OnClickListener() {
....... });

- In AlertDialog there could be either 1 or 2 or 3 buttons, and you can set up each and their click functions as well.

alertDialog.setIcon(R.drawable.search);
- You can change the icon of the AlertDialog using this line.

alertDialog.show();
- This is the line where you show the AlertDialog we had created

Source
Main.xml of Displaying AlertDialog in Android
Main.java of Displaying AlertDialog in Android

4 comments:

RAFAEL SACCOMANI said...

thanks for this article!

Charlie Collins said...

Useful example, thanks. One note though, you can simplify this some by using the Builder pattern, that is what the "Builder" here is for - such as this:

new AlertDialog.Builder(this).setTitle("Alert 1").setMessage("This is an alert").create();

and so on. It's creating the same thing you are, of course, it's just a bit more compact and convenient to use the Builder style.

Richard said...

doesnt even work, will not compile, incomplete example

Almond said...

@richard ofcourse its not a complete example, you try to learn how to compile first before you try it. Thanks