Reputation: 7348
I want to display a AlertDialog
with a list of items. The list should be two dimensional. On pressing the a button the dialog should be displayed. So how should i do it? Is there a need to create a xml file seperately for the alert dialog or should I include the dialog in java code itself?
Upvotes: 0
Views: 616
Reputation: 3121
Why don't you create a dialog themed activity and pop it up instead of the Dialog?
If you insist on creating a Dialog. Here is a piece of code that you can try out.
//Class Level Variables:
CharSequence[] items = { "Google", "Apple", "Microsoft" };
boolean[] itemsChecked = new boolean [items.length];
//Call this when you want a dialog
showdialog(0);
//override onCreateDialog
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("This is a dialog with some simple text...")
.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getBaseContext(),
items[which] + (isChecked ? " checked!": " unchecked!"),
Toast.LENGTH_SHORT).show();
}
}
)
.create();
}
This creates an AlertDialog which has a checkbox and name.....
Upvotes: 0
Reputation: 7031
To create Alert Dialog,
public void Alert(String text, String title)
{
AlertDialog dialog=new AlertDialog.Builder(context).create();
dialog.setTitle(title);
dialog.setMessage(text);
if(!title.equals("") && !text.equals(""))
{
dialog.setButton("OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//
}
});
dialog.setButton2("Cancel",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//
}
});
}
dialog.show();
}
Upvotes: 3