Skip to content

Half Dozen Hello Worlds Part 3 – Using Popup Notifications

AlertDialog Buttons

In SDK 1.1 there were three methods we used to make buttons in our AlertDialog: setButton, setButton2, and setButton3.  In our example here we are only using setButton.  That will give us a single button that is centered horizontally in the AlertDialog.  If we also called setButton2 we would have two buttons side by side with our first button on the left and our second button on the right.  If we include all three buttons like this:

        AlertDialog helloAlert = new AlertDialog.Builder(this).create();
        helloAlert.setTitle("Half Dozen Hello Worlds Demo");
        helloAlert.setMessage("Hello World");
        helloAlert.setButton("Button", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.setButton2("Button2", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.setButton3("Button3", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.show();

We would see our first button on the far left, the second on the far right, and the third in the middle like so.

AlertDialog Buttons

If the numbering seems a little arbitrary to you then you may not be alone since these methods were deprecated.  Let’s build this code using a more recent Android SDK.  Right click on your project, and click on Properties.  Click on Android, and click on the check mark next to either Android 1.5, Android 1.6, or Android 2.0.  Then click Apply, and then OK.

17-Project Properties

Go to your code and replace this code block

helloAlert.setButton("Button", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.setButton2("Button2", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.setButton3("Button3", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});

With this:

        helloAlert.setButton(AlertDialog.BUTTON_POSITIVE, "Button", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.setButton(AlertDialog.BUTTON_NEGATIVE,"Button2", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.setButton(AlertDialog.BUTTON_NEUTRAL ,"Button3", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});

You can see that the new method for adding buttons is to call one single method (setButton) and pass a constant telling Android how to render it. 

This is the end of our introduction to popup notifications, but come back for Part 4 where we will use Text to Speech to make your Android Device actually say “Hello World.”

{ 5 } Comments