Skip to content

Half Dozen Hello Worlds Part 3 – Using Popup Notifications

Alerts

When I was a kid I used to love The Great Muppet Caper.  I vividly remember a scene where Gonzo shouts, “Stop the Presses!”  The editor asks him what happened and he says, “I just always wanted to say that.”  If you want a pop-up that makes sure it gets attention (whether something important happened or not) then an Alert might be just what you need.  Let’s update our Java Activity to look like this:

package com.learnandroid.helloworld;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
 
public class Hello3 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        AlertDialog helloAlert = new AlertDialog.Builder(this).create();
        helloAlert.setTitle("Half Dozen Hello Worlds Demo");
        helloAlert.setMessage("Hello World");
        helloAlert.setButton("Close", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {}
		});
        helloAlert.show();
    }
}

The lines we’ve added are:

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

The AlertDialog Class makes use of the Builder Class AlertDialogBuilder.  So in the first line when we call:

AlertDialog helloAlert = new AlertDialog.Builder(this).create();

We are using the Builder to return an AlertDialog object to us that is already partially setup for our use.  The next two lines use setTitle and setMessage to set the text displayed to the user in the dialog’s title section and body respectively.  Finally, we need to have a way for the user to actually interact with our alert.  At minimum we should have a close button so the user can make the alert go away. We use setButton, passing it the text that should be displayed on the button, and an event that allows us to perform actions when the button is clicked.  By default, the dialog will be closed on a click event so we don’t need to put any code into our event.  Finally we call the show method, like we did with toast, to display our Hello World pop-up.

While that technically finishes both of our Hello World pop-up programs I want to take a moment to examine the buttons on the AlertDialog.

{ 5 } Comments

  1. triki | July 8, 2010 at 10:37 pm | Permalink

    Thanks for this helpful tutorial !!!

  2. Sid | January 25, 2011 at 7:35 pm | Permalink

    I tried this example but this won’t render a toast message, is anything else required here?

    Thanks,
    Sid

  3. Sid | January 25, 2011 at 8:02 pm | Permalink

    Got it working..

    Thanks,
    Sid

  4. Nicholas Johnson | June 11, 2012 at 2:09 pm | Permalink

    I really like this tutorial set. You have a very engaging style.

  5. vivien | June 11, 2014 at 2:51 am | Permalink

    thank you very much!

{ 2 } Trackbacks

  1. […] * Website Comment « Half Dozen Hello Worlds Part 1 – Your First Android Project Half Dozen Hello Worlds Part 3 – Using Popup Notifications […]

  2. […] Part 3 showed you two different kinds of pop-up message. […]

Post a Comment

Your email is never published nor shared. Required fields are marked *