Bug 774648 - Enable positive button in MultiChoicePreference only if items are checked. r=mfinkle a=akeybl
authorBrian Nicholson <bnicholson@mozilla.com>
Wed, 18 Jul 2012 10:11:26 -0700
changeset 102151 985eaf394b5208d41244421f5f5d2d6ca5ec62e4
parent 102150 be0f058891bc7d226394cb84e5847ea996a0d05b
child 102152 285eb81b436793f7359cb2cfec6306070918bd3d
push id1747
push userbnicholson@mozilla.com
push dateMon, 23 Jul 2012 18:25:56 +0000
treeherdermozilla-aurora@e38f3d164079 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, akeybl
bugs774648
milestone16.0a2
Bug 774648 - Enable positive button in MultiChoicePreference only if items are checked. r=mfinkle a=akeybl
mobile/android/base/MultiChoicePreference.java
--- a/mobile/android/base/MultiChoicePreference.java
+++ b/mobile/android/base/MultiChoicePreference.java
@@ -1,23 +1,25 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
+import android.app.AlertDialog;
 import android.app.AlertDialog.Builder;
 import android.content.res.TypedArray;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.preference.DialogPreference;
 import android.preference.PreferenceManager;
 import android.util.AttributeSet;
 import android.util.Log;
+import android.widget.Button;
 
 class MultiChoicePreference extends DialogPreference {
     private static final String LOGTAG = "GeckoMultiChoicePreference";
 
     private boolean mValues[];
     private boolean mPrevValues[];
     private CharSequence mEntryKeys[];
     private CharSequence mEntries[];
@@ -145,16 +147,28 @@ class MultiChoicePreference extends Dial
         if (mEntries.length != mEntryKeys.length || mEntryKeys.length != mInitialValues.length) {
             throw new IllegalStateException(
                     "MultiChoicePreference entries, entryKeys, and initialValues arrays must be the same length");
         }
 
         builder.setMultiChoiceItems(mEntries, mValues, new DialogInterface.OnMultiChoiceClickListener() {
             public void onClick(DialogInterface dialog, int which, boolean val) {
                 // mValues is automatically updated when checkboxes are clicked
+
+                // enable positive button only if at least one item is checked
+                boolean enabled = false;
+                for (int i = 0; i < mValues.length; i++) {
+                    if (mValues[i]) {
+                        enabled = true;
+                        break;
+                    }
+                }
+                Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
+                if (button.isEnabled() != enabled)
+                    button.setEnabled(enabled);
             }
         });
     }
 
     @Override
     protected void onDialogClosed(boolean positiveResult) {
         if (!positiveResult) {
             // user cancelled; reset checkbox values to their previous state