Search This Blog

22 April 2010

Alert Dialogs

Using our last example as a starting point, we are going to add an alert dialog.  Make sure to do a 'mvn install' before we get started so your IDE doesn't try to guess where R.layout.main is.

Open MyActivity.java. Let's modify the onCreate method:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("It's an alert!");
        dialog.setMessage("This alert has a message.");
        dialog.setIcon(R.drawable.icon);
        dialog.setButton("OK", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                return;
            }
        });
        dialog.show();
    }

Redeploy (mvn clean install) and you'll see the alert when you launch your app.

1 comment:

  1. There is also a dialog.setButton2 and dialog.setButton3. Surprisingly, Button3 seems to be in the middle.

    ReplyDelete