How can I add a third button to an Android Alert Dialog?

user180825 picture user180825 · Jan 12, 2011 · Viewed 85k times · Source

The API says that the Alert Dialog can have one, two or three buttons, but the SDK only allows for a positive and negative button. How then can I add a third button?

Answer

ninjasense picture ninjasense · Jan 12, 2011

When you are creating the dialog, add something like this to the builder:

builder = new AlertDialog.Builder(context);
builder.setTitle("Test");
builder.setIcon(R.drawable.icon);
builder.setMessage("test");
builder.setPositiveButton("Call Now",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });

builder.setNeutralButton("Setup",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                context.startActivity(new Intent(context, Setup.class));
                //dialog.cancel();
            }
        });

builder.setNegativeButton("Exit",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });
builder.create().show();