Thursday, July 19, 2012

Selection list in alert dialog box - Android

- Kalimuthu Rengaswamy
Dialog box is one of the most interesting feature in Android. The style and way making it appear depents upon the type of dialog box used and how well we customize it. This blog explains how to create a list of selectable items in an alert dialog box.

Dialog class in Android
Dialog class has 4 sub classes
1. AlertDialog
2. ProgressDialog : A dialog that displays a progress wheel or progress bar.
3. DatePickerDialog : A dialog that allows the user to select a date.
4. TimePickerDialog : A dialog that allows the user to select a time.

Alert dialog
Alert dialog as the name simplies shows an alert to the user. It can manage at the maximum of 3 buttons, a selectable list of items that can even include multiple choice.

Features of alert dialog
1. A title
2. A text message
3. One, two, or three buttons
4. A list of selectable items (with optional checkboxes or radio buttons)  

Creating alert dialog

Single choice alert:
// this declare in golbal value
public int postion
final AlertDialog.Builder singlechoicedialog = new AlertDialog.Builder(this);
final CharSequence[] Report_items = { "Choice I", "Choice II", "Choice III","Choice IV", "Choice V" };
singlechoicedialog.setTitle("SingleChoice Dialog ");
singlechoicedialog.setSingleChoiceItems(Report_items, -1,
new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int item) {
 postion = item;

 // get selected value
 String value = Report_items[item].toString()
 System.out.println("Selected position::" + value);
 dialog.cancel();
 }
});
AlertDialog alert_dialog = singlechoicedialog.create();
alert_dialog.show();

// set defult select value
alert_dialog.getListView().setItemChecked(postion, true);



Multi choice alert:

final CharSequence[] dilogList = { "Choice One", "Choice Two", "Choice Three" };

AlertDialog.Builder multChoiceDialog = new AlertDialog.Builder(this);



//set title for aleart box

multChoiceDialog.setTitle("MultiChoice Dialog");

boolean[] _selections = new boolean[dilogList.length];

multChoiceDialog.setMultiChoiceItems(dilogList, _selections, new DialogInterface.OnMultiChoiceClickListener() {
 public void onClick(DialogInterface dialog,
 int whichButton, boolean isChecked) {
 }
});

// add positive button here
multChoiceDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 // getting listview from alert box
 ListView list = ((AlertDialog) dialog).getListView();
 StringBuilder sb = new StringBuilder();
 for (int i = 0; i < list.getCount(); i++) {
  boolean checked = list.isItemChecked(i);
  // get checked list value
  if (checked) {
   if (sb.length() > 0)
    sb.append(",");
    sb.append(list.getItemAtPosition(i));
  }
 }

 Toast.makeText(getApplicationContext(),"Selected digit:"
 +sb.toString(),Toast.LENGTH_SHORT).show();
 }
});

// add negative button
multChoiceDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
 @Override
 public void onClick(DialogInterface dialog, int which) {
 // cancel code here
 }
});
AlertDialog alert1 = multChoiceDialog.create();
alert1.show();


List alert:

final AlertDialog.Builder menuAleart = new        AlertDialog.Builder(FiltterlistActivity.this);
final String[] menuList = { "function1", "function2" };
menuAleart.setTitle("list dialog");
menuAleart.setItems(menuList,new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int item) {
  switch (item) {
  case 0:
  // function 1 code here
  break;
  case 1:
  // function 2 code here
  break;
  }
 }
});
AlertDialog menuDrop = menuAleart.create();
menuDrop.show();


Here, I have picked a single feature of alert dialog and explained. You can just copy, paste and execute the code for the results.  





37 comments:

Anonymous said...

Nice One Dude.. Its really help me ..keep the good work ..:)

Saru's creation said...

congrats. da.. i shared ur link to my friens.. :)

Unknown said...

very nice, it's help me
thanks... :)

Richard Majece said...

If you want to learn more about chemistry, try to read https://pro-essay-writer.com/blog/chemistry-study. It was really useful for me

Ramona Brekke said...

Be more aware of resume length questions and answers and it helps you to achieve more goals connected to your job.

katebrouwn said...

It was useful for me to read about this. Thanks for sharing. I try to keep up to date with new products and a site like yours help me. In fact, I can help you with argumentative essay animal testing and answers if you want.

McAfee Activate said...

I agree with your opinion, I'm sure your ideas will be successful in the future!
It is one of the best sites that I have visited. I hope you will share more quality blog posts thank you.
While using any application if there is an issue that occurs like hacking and the application is processing slow then contact McAfee account login to get the instant solution.

Quickbook Support said...

Great Blog!!! I love all the blog posts. Quickbooks desktop is one of the most popular software service provider in USA. It is the Quickbooks contactbest programming where the user can store the accounting transaction related information.

Eima Jackson said...

You have published an informative and effective post. Mostly, I visit on your every blog. If you are getting any type of QuickBooks error 15215 code window 10 on your windows screen, dial the QB error support number (+1)-877-756-9341 to get rid of instantly.

Tim said...

write my discussion board post service available 24/7

shalley30 said...

There are a few stages to introduce norton antivirus and join on norton website.Go to join letter box click connect. This is an initiation keys for Norton Users. Recharge your Norton antivirus with item key from fill and enact norton antivirus in system.

norton.com/setup

Mike77 said...

I would like to introduce you to an excellent service that at one time helped me write my essay. I don’t even know what I’m doing. If it weren’t for them, as the deadlines are already running out Pay for Essay

Sumona Tiger said...

For Activate your Office 365 visit office setup website from any browser, Sign in with your microsoft account with email and password, if you don't have account please create one, Then enter your 25 digits office product key to get started.

office.com/setup | www.office.com/setup

LasseNielsen said...

Thanks to writers from Essaywriter service for the quick and professional help in writing my essay. I often use such writing services, as it is very difficult for me to write essays and some other types of academic papers. A few weeks ago I ordered an essay at Essaywriter service. The paper was written quickly and professionally. It was also very convenient for me to select and order the necessary paper, as the site has convenient and advanced functionality. I will use this service to order other essays.

augustwalker said...

epson error 031008 how to fix? Dial for Online Solution to Fix Epson Printer Error Code 031008 and Epson Error Code 031008 .

Unknown said...

We place a high value on establishing long-term relationships with our clients, eventually becoming virtual extensions of their organizations. Our consultants and engineering teams address our clients' specific requirements with best-in-class support solutions across a broad scale of service areas. Address : 1010 N. Central Ave,Glendale, CA 91202,USA Toll free no : 1-909-616-7817.

meldaresearch said...

Finding the best English paper writing services and English Language Writing Help is not easy unless one is keen to establish top ranked english language essay writing service provider & English editing & proofreading services.

Jeffrey said...

Great article, very helpful. I also want to offer a way out of the situation in writing coursework, reviews, presentations, and more. https://topdissertations.com/order-dissertation/ the best friend in writing the author's works!

Dim4ksan said...

Recently, a lot of works I order edubirdie review because of its good quality. Let's take for example my last work. It was a great philosophy essay, which was rated as the best among the whole group.

jackson said...

I have been to many dating sites, but only brazilian brides helped me. I was picked up a beautiful young girl.

QuickBooks Support Phone Number said...

Great work. Thank you for sharing such an informative post. In case you are struggling with QuickBooks Errors, Call now at QuickBooks Support Phone Number+1-800-496-0147 and get instant help and support from our professionals. quickbooks online support phone number

Cheryl Marrero said...

Thanks fomr this amazing blog I loved reading it.
If you facing troubles with QuickBooks, you can reach QuickBooks error support or Reach me at: +1-800-496-0147

Quickbooks Expert said...

Nice post!

Worried About QuickBooks Error ?Get in touch with QuickBooks expert for instant solution.
Click Here to know how to fix QuickBooks Error 15241

Dial on QuickBooks Error Support Number +1-855-977-7463.

Quickbooks Expert said...

Nice & Informative Blog !
For managing accounting tasks, you should use QuickBooks accounting software.In case you have faced any technical issues in QuickBooks, call us at QuickBooks Customer Service Number 1-877-948-5867.

Elena James said...

Very inspiring Article. Thank you for sharing this information. I hope it will be really helpful for all the readers on your website.
QuickBooks error code list
QuickBooks error code 404
QuickBook error 15215

QuickBooks Support Number said...

Nice Blog !
QuickBooks Payroll Error PS036 is an annoying error that restricts all your ongoing operations.Our team will provide you with feasible solutions that not only solve your issue but also prevent them from occurring again in the future.

kalamena said...

Unfortunately, it becomes harder to buy wife online. Although many internet contacts have occurred in these days, it is still a difficult work to discover a female. However, there is a website where you may purchase a wife online and avoid all the hassle of locating a woman. The guys of this service will discover a lady for all tastes. I had just been in contact with a gorgeous lady for a few days when I used their services My friends who had a hard time looking for a wife with their timely help and low pricing might suggest this business.

rabekahmarshall said...

Really Informative post. Thank you so much for sharing this valuable info with us.


If you want best research paper writing service then do follow the link for best writing service available online.

Tommy(R) said...

Previously, I had very big problems with my studies, because of which the teachers, so to speak, "tried to humiliate." But once again climbing the worldwide Internet grid (Google), I came across one forum. On this forum they talked about one site. This site is myexamcoach.com. This site will help you with any exam or session. After I got to this site, my studies went up! So I personally recommend this site!

SAVIOLA said...

This post is the most incredible article I haven't read in a very long time. I hope to read more from this blog. Click to see tasu cut off mark

SAVIOLA said...

You’re incredible! Thank you! This blog is a very informative place. I'll come by often. Also check ...is funaab post utme form out

Brakly Willom said...

Thanks for sharing such useful information with us. I hope you will share some more info about of QuickBooks Enterprise Support (855)756-1077. Please keep sharing. We will also provide QuickBooks Customer Service Number (855)538-8273 for instant help.

TAZEEN said...

Technologistan is the popoular and most trustworthy resource for technology, telecom, business and auto news in Pakistan
oppo f17 price in pakistan

Issac Martin said...

Such a great post! Thanks for sharing relevant information. Visit for more information. amc stocktwits

JKM SOFT SOLUTION said...

Search engine optimization california is the need of the hour in order to help you grow your business exponentially.

Apostille said...

MEA Apostille Attestation is finished individual reports like Apostille Birth Certificate, Death Certificate Apostille, Marriage Certificate Apostille, Educational Degree Certificate Apostille Authentication, and so on

Writer said...

Thank you for your post! When I was busy with a movie critique outline in my essay I asked for help online. I can recommend now a very cool writer who will solve any writing trouble for you. Just contact me.