Use AlertDialog.Builder to build an AlertDialog
Description
The following code shows how to Use AlertDialog.Builder to build an AlertDialog.
Example
package com.java2s.myapplication3.app;
//from w w w . j a v a2 s . c om
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity
implements DialogInterface.OnClickListener, View.OnClickListener {
private static final String[] ZONES =
{"Pacific Time", "Mountain Time", "Central Time", "Eastern Time", "Atlantic Time"};
Button mButton;
AlertDialog mActions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Activity");
mButton = new Button(this);
mButton.setText("Click for Time Zones");
mButton.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Time Zone");
builder.setItems(ZONES, this);
builder.setNegativeButton("Cancel", null);
mActions = builder.create();
setContentView(mButton);
}
@Override
public void onClick(DialogInterface dialog, int which) {
String selected = ZONES[which];
mButton.setText(selected);
}
@Override
public void onClick(View v) {
mActions.show();
}
}