Amazon Gift Cards

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.

Update
Jun 12, 2011 - I created a small tool to generate code for this at http://www.generateandroidcode.com/android/alert

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

26 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

Ranjan Deo said...

Example is very good.
To make it work, setButton should be replaced with setPositivieButton or setNegativeButton or setNeutralButton, then it will work perfectly.

Bennox said...

im new in android, and this comment its nice,thanks.

A.V.Ebrahimi said...

Thanks for quick explanation.

Anonymous said...

Simple and very good example!

Thanks

Anonymous said...

You could also try a rich dialog using a webview. See me tutorial: http://www.dev-smart.com/?p=9

Keerthy said...

To get more details, you can also visit http://android-codes-examples.blogspot.com/2011/03/how-to-display-alertdialog-and.html

MAD'u CrY said...

In alert dialog I am put 2 buttons as setPositive and negative buttons . And now I want to disable the positive button by default initially.
Ia it possible to do?

mahendra said...

it good tutorial

but i want create multi line Dilogbox
for show add

plz help in these case....

Almond said...

Check http://www.monmonja.com/android/alert

Chadwick said...

Awesome Almmond! Thanks!

fifa world cup schedule said...

Thanks for a very useful example. But your link is not working.

Anonymous said...

thanks ! simple and clear.

Club Car said...

Good Information regarding the alert dialog tutorial on android. Last night I have done some good stuff but have seen some error while I have click on the button. After that I differentiate your program and my program and got solved. It was that I have not added the .create method in the alert dialog so it was error

Anonymous said...

What if I don't want to use a (totally useless) final/fixed string array constant?

I need to add/remove items from my array, and then redisplay my AlertDialog.

Anonymous said...

Is there a way to specify:
Just display this text...and 1 okbutton...and just close the alert when the user hits "ok".

(Listeners = none)

Anonymous said...

very useful, ty m8

Viral Patel said...

very helpful. thanks!

Anonymous said...

Great post.

But how i can save the message of the alert in a variable?
I have a interface with an webservice, i will receive True or False on this message.
How can i verify what message is?

Regards.

Jay said...

DialogInterface gives me an error when i run it on eclipse, telling me Unidentified Keyword.

any idea?

Anonymous said...

Doesn't compile :(

abhi019 said...

works fine...

thanks a lot at last something i got workable for alert boxes

Alec Lajoie said...

Is it possible to set the message in the AlertDialouge to an onclick or make the text linkify? Thanks.