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?
Asked
Active
Viewed 609 times
0

Chilledrat
- 2,593
- 3
- 28
- 38

thedarkpassenger
- 7,158
- 3
- 37
- 61
-
If you know how to define custom adapter then it is easy for you to define custom dialog as you have mentioned. – Paresh Mayani May 24 '12 at 05:16
-
u hav to to create dialog....which will include ur created xmlfile – c2dm May 24 '12 at 05:18
-
using `setView()` method, you can set any view to your alert dialog. – Paresh Mayani May 24 '12 at 05:19
-
http://developer.android.com/guide/topics/ui/dialogs.html – Krishnakant Dalal May 24 '12 at 05:25
-
you checked this answer? http://stackoverflow.com/a/10652886/1168654 – Dhaval Parmar May 24 '12 at 05:46
2 Answers
3
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();
}

Ponmalar
- 6,871
- 10
- 50
- 80
-
thanks for the code but which package should i include for DialogInterface...because it is showing some error on Dialog Interface – thedarkpassenger May 24 '12 at 05:34
-
-
thanks and its working...What i have to do to include 2 items in each row in a list? – thedarkpassenger May 24 '12 at 05:45
-
refer this http://iserveandroid.blogspot.in/2010/12/custom-gridview-in-dialog.html – Ponmalar May 24 '12 at 05:47
-
this makes use of a gridview..is there any other method out so that it shows two elements without the help of gridview – thedarkpassenger May 24 '12 at 06:05
-
you meant you want to show multible lines ah? if yes http://stackoverflow.com/questions/5913166/displaying-multiple-lines-of-text-and-variables-in-an-alertdialog-using-setmessa – Ponmalar May 24 '12 at 06:12
-
suppose i have three elements red, blue and green.Now i want to add a image with all of three elements..how is it posible ..every row with show the name and colour of element – thedarkpassenger May 24 '12 at 06:16
-
Check this: http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/ – Ponmalar May 24 '12 at 06:27
0
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.....

drulabs
- 3,071
- 28
- 35