And_dev
And_dev

Reputation: 179

Android : Alert Dialog with Multi Choice

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);
         }

      }
    });        

Upvotes: 4

Views: 6899

Answers (3)

timonvlad
timonvlad

Reputation: 1056

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();

Upvotes: -1

Dan Hulme
Dan Hulme

Reputation: 15290

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.

Upvotes: 4

Roman Marius
Roman Marius

Reputation: 476

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();

Upvotes: 0

Related Questions