@@ -111,7 +111,12 @@ public class MaterialAutoCompleteTextView extends AutoCompleteTextView {
111
111
private int errorColor ;
112
112
113
113
/**
114
- * characters count limit. 0 means no limit. default is 0. NOTE: the character counter will increase the View's height.
114
+ * min characters count limit. 0 means no limit. default is 0. NOTE: the character counter will increase the View's height.
115
+ */
116
+ private int minCharacters ;
117
+
118
+ /**
119
+ * max characters count limit. 0 means no limit. default is 0. NOTE: the character counter will increase the View's height.
115
120
*/
116
121
private int maxCharacters ;
117
122
@@ -260,6 +265,7 @@ public MaterialAutoCompleteTextView(Context context, AttributeSet attrs, int sty
260
265
primaryColor = typedArray .getColor (R .styleable .MaterialEditText_primaryColor , defaultPrimaryColor );
261
266
setFloatingLabelInternal (typedArray .getInt (R .styleable .MaterialEditText_floatingLabel , 0 ));
262
267
errorColor = typedArray .getColor (R .styleable .MaterialEditText_errorColor , Color .parseColor ("#e7492E" ));
268
+ minCharacters = typedArray .getInt (R .styleable .MaterialEditText_minCharacters , 0 );
263
269
maxCharacters = typedArray .getInt (R .styleable .MaterialEditText_maxCharacters , 0 );
264
270
singleLineEllipsis = typedArray .getBoolean (R .styleable .MaterialEditText_singleLineEllipsis , false );
265
271
helperText = typedArray .getString (R .styleable .MaterialEditText_helperText );
@@ -421,7 +427,7 @@ private void initPadding() {
421
427
* calculate {@link #minBottomLines}
422
428
*/
423
429
private void initMinBottomLines () {
424
- boolean extendBottom = maxCharacters > 0 || singleLineEllipsis || tempErrorText != null || helperText != null ;
430
+ boolean extendBottom = minCharacters > 0 || maxCharacters > 0 || singleLineEllipsis || tempErrorText != null || helperText != null ;
425
431
currentBottomLines = minBottomLines = minBottomTextLines > 0 ? minBottomTextLines : extendBottom ? 1 : 0 ;
426
432
}
427
433
@@ -603,6 +609,17 @@ public void setMaxCharacters(int max) {
603
609
postInvalidate ();
604
610
}
605
611
612
+ public int getMinCharacters () {
613
+ return minCharacters ;
614
+ }
615
+
616
+ public void setMinCharacters (int min ) {
617
+ minCharacters = min ;
618
+ initMinBottomLines ();
619
+ initPadding ();
620
+ postInvalidate ();
621
+ }
622
+
606
623
public int getErrorColor () {
607
624
return errorColor ;
608
625
}
@@ -647,7 +664,7 @@ public CharSequence getError() {
647
664
* only used to draw the bottom line
648
665
*/
649
666
private boolean isInternalValid () {
650
- return tempErrorText == null && isMaxCharactersValid ();
667
+ return tempErrorText == null && isCharactersCountValid ();
651
668
}
652
669
653
670
/**
@@ -811,9 +828,17 @@ protected void onDraw(@NonNull Canvas canvas) {
811
828
float fontPaddingTop = floatingLabelTextSize + fontMetrics .ascent + fontMetrics .descent ;
812
829
813
830
// draw the characters counter
814
- if ((hasFocus () && maxCharacters > 0 ) || !isMaxCharactersValid ()) {
815
- textPaint .setColor (isMaxCharactersValid () ? getCurrentHintTextColor () : errorColor );
816
- String text = getText ().length () + " / " + maxCharacters ;
831
+ if ((hasFocus () && hasCharatersCounter ()) || !isCharactersCountValid ()) {
832
+ textPaint .setColor (isCharactersCountValid () ? getCurrentHintTextColor () : errorColor );
833
+ String text ;
834
+ if (minCharacters <= 0 ) {
835
+ text = getText ().length () + " / " + maxCharacters ;
836
+ } else if (maxCharacters <= 0 ) {
837
+ text = getText ().length () + " / " + minCharacters + "+" ;
838
+ } else {
839
+ text = getText ().length () + " / " + minCharacters + "-" + maxCharacters ;
840
+ }
841
+
817
842
canvas .drawText (text , getWidth () + getScrollX () - textPaint .measureText (text ), lineStartY + bottomSpacing + relativeHeight , textPaint );
818
843
}
819
844
@@ -871,11 +896,15 @@ private int getBottomTextLeftOffset() {
871
896
}
872
897
873
898
private int getBottomTextRightOffset () {
874
- return maxCharacters > 0 ? (int ) textPaint .measureText ("00/000" ) : 0 ;
899
+ return hasCharatersCounter () ? (int ) textPaint .measureText ("00/000" ) : 0 ;
900
+ }
901
+
902
+ public boolean isCharactersCountValid () {
903
+ return !hasCharatersCounter () || getText () == null || getText ().length () == 0 || (getText ().length () >= minCharacters && (maxCharacters <= 0 || getText ().length () <= maxCharacters ));
875
904
}
876
905
877
- public boolean isMaxCharactersValid () {
878
- return maxCharacters <= 0 || getText () == null || getText (). length () <= maxCharacters ;
906
+ private boolean hasCharatersCounter () {
907
+ return minCharacters > 0 || maxCharacters > 0 ;
879
908
}
880
909
881
910
@ Override
0 commit comments