Browse Source

Improvements to the input dialog, also added max length capability with an indicator and error color. Resolves https://github.com/afollestad/material-dialogs/issues/487.

Aidan Follestad 10 years ago
parent
commit
a0fc97834b

+ 10 - 0
library/src/main/java/com/afollestad/materialdialogs/DialogInit.java

@@ -5,6 +5,7 @@ import android.content.res.ColorStateList;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.os.Build;
+import android.text.InputFilter;
 import android.text.InputType;
 import android.text.method.LinkMovementMethod;
 import android.text.method.PasswordTransformationMethod;
@@ -383,6 +384,15 @@ class DialogInit {
                 dialog.input.setTransformationMethod(PasswordTransformationMethod.getInstance());
             }
         }
+
+        dialog.inputMinMax = (TextView) dialog.view.findViewById(R.id.minMax);
+        if (builder.inputMaxLength > -1) {
+            dialog.inputMinMax.setText("0/" + builder.inputMaxLength);
+            dialog.inputMinMax.setTextColor(builder.contentColor);
+        } else {
+            dialog.inputMinMax.setVisibility(View.GONE);
+            dialog.inputMinMax = null;
+        }
     }
 
     private static ColorStateList getActionTextStateList(Context context, int newPrimaryColor) {

+ 31 - 1
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -38,6 +38,7 @@ import android.widget.TextView;
 
 import com.afollestad.materialdialogs.internal.MDButton;
 import com.afollestad.materialdialogs.internal.MDRootLayout;
+import com.afollestad.materialdialogs.internal.MDTintHelper;
 import com.afollestad.materialdialogs.util.DialogUtils;
 import com.afollestad.materialdialogs.util.TypefaceHelper;
 
@@ -64,6 +65,7 @@ public class MaterialDialog extends DialogBase implements
     protected TextView mProgressMinMax;
     protected TextView content;
     protected EditText input;
+    protected TextView inputMinMax;
 
     protected MDButton positiveButton;
     protected MDButton neutralButton;
@@ -388,6 +390,8 @@ public class MaterialDialog extends DialogBase implements
         protected boolean inputAllowEmpty;
         protected int inputType = -1;
         protected boolean alwaysCallInputCallback;
+        protected int inputMaxLength = -1;
+        protected int inputMaxLengthErrorColor = 0;
 
         protected boolean titleColorSet = false;
         protected boolean contentColorSet = false;
@@ -1042,6 +1046,21 @@ public class MaterialDialog extends DialogBase implements
             return this;
         }
 
+        public Builder inputMaxLength(int maxLength, int errorColor) {
+            if (maxLength < 1)
+                throw new IllegalArgumentException("Max length for input dialogs cannot be less than 1.");
+            this.inputMaxLength = maxLength;
+            this.inputMaxLengthErrorColor = errorColor;
+            return this;
+        }
+
+        /**
+         * Same as #{@link #inputMaxLength(int, int)}, but it takes a color resource ID for the error color.
+         */
+        public Builder inputMaxLengthRes(int maxLength, @ColorRes int errorColor) {
+            return inputMaxLength(maxLength, context.getResources().getColor(errorColor));
+        }
+
         public Builder alwaysCallInputCallback() {
             this.alwaysCallInputCallback = true;
             return this;
@@ -1389,9 +1408,20 @@ public class MaterialDialog extends DialogBase implements
             public void onTextChanged(CharSequence s, int start, int before, int count) {
                 if (mBuilder.alwaysCallInputCallback)
                     mBuilder.inputCallback.onInput(MaterialDialog.this, s);
+                final int length = s.toString().trim().length();
                 if (!mBuilder.inputAllowEmpty) {
                     final View positiveAb = getActionButton(DialogAction.POSITIVE);
-                    positiveAb.setEnabled(s.toString().trim().length() > 0);
+                    positiveAb.setEnabled(length > 0);
+                }
+                if (inputMinMax != null) {
+                    inputMinMax.setText(length + "/" + mBuilder.inputMaxLength);
+                    final boolean overMax = length > mBuilder.inputMaxLength;
+                    final int colorText = overMax ? mBuilder.inputMaxLengthErrorColor : mBuilder.contentColor;
+                    final int colorWidget = overMax ? mBuilder.inputMaxLengthErrorColor : mBuilder.widgetColor;
+                    inputMinMax.setTextColor(colorText);
+                    MDTintHelper.setTint(input, colorWidget);
+                    final View positiveAb = getActionButton(DialogAction.POSITIVE);
+                    if (positiveAb.isEnabled()) positiveAb.setEnabled(!overMax);
                 }
             }
 

+ 32 - 8
library/src/main/res/layout/md_dialog_input.xml

@@ -25,19 +25,43 @@
             android:fontFamily="sans-serif"
             android:textSize="@dimen/md_content_textsize"
             android:layout_marginTop="4dp"
-            android:layout_marginBottom="12dp"
-            android:layout_marginLeft="2dp"
-            android:layout_marginRight="2dp"
+            android:layout_marginBottom="@dimen/md_content_padding_bottom"
             tools:text="Message"
             android:layout_gravity="center_horizontal"
             tools:ignore="UnusedAttribute" />
 
-        <EditText
-            android:id="@android:id/input"
+        <RelativeLayout
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:textSize="@dimen/md_content_textsize"
-            tools:ignore="TextFields" />
+            android:layout_height="wrap_content">
+
+            <EditText
+                android:id="@android:id/input"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:textSize="@dimen/md_content_textsize"
+                tools:ignore="TextFields"
+                android:layout_marginLeft="-2dp"
+                android:layout_marginRight="-2dp"
+                android:layout_marginBottom="2dp" />
+
+            <TextView
+                android:id="@+id/minMax"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:minWidth="48dp"
+                tools:text="50/100"
+                android:textSize="@dimen/md_content_textsize"
+                android:gravity="end"
+                android:textAlignment="viewEnd"
+                android:layout_alignRight="@android:id/input"
+                android:layout_alignEnd="@android:id/input"
+                android:layout_below="@android:id/input"
+                android:paddingRight="4dp"
+                android:paddingEnd="4dp"
+                android:fontFamily="sans-serif-medium"
+                tools:ignore="RtlSymmetry,UnusedAttribute" />
+
+        </RelativeLayout>
 
     </LinearLayout>
 

+ 2 - 0
sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.java

@@ -599,6 +599,8 @@ public class MainActivity extends AppCompatActivity implements
                 .inputType(InputType.TYPE_CLASS_TEXT |
                         InputType.TYPE_TEXT_VARIATION_PERSON_NAME |
                         InputType.TYPE_TEXT_FLAG_CAP_WORDS)
+                .inputMaxLengthRes(20, R.color.material_indigo_600)
+                .positiveText(R.string.submit)
                 .input(R.string.input_hint, 0, false, new MaterialDialog.InputCallback() {
                     @Override
                     public void onInput(MaterialDialog dialog, CharSequence input) {

+ 1 - 0
sample/src/main/res/values/strings.xml

@@ -154,5 +154,6 @@
     <string name="simple_list">Simple Dialog (Guidelines Spec)</string>
     <string name="set_backup">Set backup account</string>
     <string name="add_account">Add account</string>
+    <string name="submit">Submit</string>
 
 </resources>