4

Is it possible to show Alert Dialog with Multi Choice with disabled items(Rows) in the list? By checking "None" Option in the list all options in the list should get disabled except option "None", if i uncheck option "None" need to enable all the items once again?

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
    dialogBuilder.setMultiChoiceItems(optionsList,selectionState,new
                                       DialogInterface.OnMultiChoiceListener()
    {

      @Override
      public void onClick(DialogInterface dialog,int which, boolean isChecked){

      final AlertDialog alertDialog = (AlertDialog) dialog;
      final ListView alertDialogList = alertDialog.getListView();

        // Here how to make the items in the list as disabled when None is clicked
        // None OPtion is one among  in optionsList string array

         // A loop to disable all items other than clicked one 
         for (int position = alertDialogList.getCheckedItemPosition(); position<
                                alertDialogList.getChildCount; position++)
         {
                alertDialogList.getChildAt(position).setEnabled(false);
         }

      }
    });        
And_dev
  • 179
  • 1
  • 5
  • 16
  • Lucifer: I tried by setting Clickable and Enable property of listview Item to false but i am unable to disable the rest of the rows in the alert dialog properly it is disabling the all the rows in the list in alert dialog. – And_dev Oct 18 '12 at 07:15
  • @And_dev:Please add your existing code to the question. – JiTHiN Oct 18 '12 at 07:43
  • @rIHaN JiTHiN: added my code snippet. – And_dev Oct 18 '12 at 09:49

3 Answers3

4

Your OnMultiChoiceClickListener is nearly there. It just has two problems: first, your for loop isn't iterating over all the children except the clicked one.

     // A loop to disable all items other than clicked one 
     for (int position = alertDialogList.getCheckedItemPosition(); position<
                            alertDialogList.getChildCount; position++)
     {
            alertDialogList.getChildAt(position).setEnabled(false);
     }

You start from the clicked one, and disable that one, then all the children after it, until the end of the list. Only children that are strictly before the clicked one don't get disabled. The second problem is that your disabling code will run for any item that's clicked, not just the 'none' item. Try something like this instead. I'm using which to identify whether the special 'none' item has been pressed.

private static final int specialItem = ...;
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (which == singleItem) { // only if they clicked 'none'
        final AlertDialog alertDialog = (AlertDialog) dialog;
        final ListView alertDialogList = alertDialog.getListView();

        for (int position = 0; position < alertDialogList.getChildCount(); position++)
        {
            if (position != which) {
                alertDialogList.getChildAt(position).setEnabled(!isChecked);
            }
        }
    }
}

Notice that I don't do anything at all if which isn't 0. My for loop starts from 1 in order to avoid item 0, and it sets every element to be enabled if the 'none' item was not checked, and disabled if the none item was checked.

Last off, I'll just note that this isn't the usual behaviour for multi-choice dialogs. The user will be surprised about the behaviour of the 'none' option, because it's different from everything else. It would be more usual to not have a 'none' option: if the user doesn't check any other option, that means none. If you really do need a 'none' option, to tell the difference between the user explicitly picking 'none' and just not answering, consider using a custom layout with a separate 'none' button or radio button that's outside the group of checkboxes, so the user can tell it will behave differently.

Dan Hulme
  • 14,779
  • 3
  • 46
  • 95
  • Thank You for your answer this is what i exactly tried but it is disabling the position '0' and leaving the position-1 as enabled. – And_dev Oct 18 '12 at 10:39
  • It may be that `AlertDialog` inserts an extra item at the top or something, so that the 'none' item is not actually in position 0, even though it's the first one you pass. Check in the debugger what value of `which` you get passed when you click on the 'none' item. – Dan Hulme Oct 18 '12 at 11:51
  • If i use this property alertDialogList.getCheckedItemPosition() i always receive -1 as position for any item clicked. – And_dev Oct 19 '12 at 05:51
  • 1
    Of course you do. The documentation for `getCheckedItemPosition()` says "The result is only valid if the choice mode has been set to `CHOICE_MODE_SINGLE`." That's why my suggested code doesn't use it. – Dan Hulme Oct 19 '12 at 10:03
  • The value of which is 0 only if i start the position value as 2 it is disabling the items from 0 to position -2. – And_dev Oct 19 '12 at 14:07
  • I've updated the example, so all you have to do is set `specialItem` to the index of the special one. I'm more convinced than ever, though, that making your GUI work this way is bad for usability. – Dan Hulme Oct 19 '12 at 17:02
  • As you said that specialItem index only i couldn't get, i'm thinking about changing this multi choice behavior as you said. Thanks for your answers. – And_dev Oct 21 '12 at 02:57
  • Dan do you have any idea about keeping a resource file in encrypted format in app and at runtime decrypt the file and using it in the application. – And_dev Oct 21 '12 at 03:17
  • If you're happy with the answer then accept it. If you have another question, go ahead and ask it in a new question so anyone can answer. – Dan Hulme Oct 21 '12 at 08:26
0
AlertDialog alertDialog  = new AlertDialog.Builder(context).create();   
            alertDialog.setTitle("Warning!");
            alertDialog.setMessage("Confirm closing activity without succes?");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {


                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    UpdateWebActivityState(ActivitiesEditActivity.this, serviceActivity.ActivityId,serviceActivity.WebActivityState , notes, sigBitmap);
                    isSuccessfullyClosed = false;
                    AlertDialog alert  = new AlertDialog.Builder(context).create(); 
                    alert.setTitle("Warning!");
                    alert.setMessage("Activity closed successfully");
                    alert.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                            do what you want here
                            finish();                   
                        }

                    });

                    alert.show();

                }
            });

            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which)
                {
                        return;
                }
                });

            alertDialog.show();
Roman Marius
  • 456
  • 3
  • 9
  • 21
-1

Yes it's real

            new AlertDialog.Builder(Main.this)
            .setIcon(R.drawable.icon)
            .setTitle("Title")
            .setView(textEntryView)
            .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //android.os.Debug.waitForDebugger();


                    /* User clicked OK so do some stuff */ 
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked cancel so do some stuff */
                }
            })
            .setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .create();
Alberto M
  • 1,608
  • 1
  • 18
  • 42
timonvlad
  • 1,046
  • 3
  • 13
  • 31
  • What is that textEntryView? i couldn't get the idea? I'm trying to show alert dialog with list of checkboxes for multi selection but when "None" option in the list get clicked rest of all rows should get disabled – And_dev Oct 18 '12 at 07:21