Skip to content

Commit ea0f255

Browse files
committed
New option: minCharacters
1 parent 42d9a31 commit ea0f255

File tree

4 files changed

+80
-20
lines changed

4 files changed

+80
-20
lines changed

library/src/main/java/com/rengwuxian/materialedittext/MaterialAutoCompleteTextView.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public class MaterialAutoCompleteTextView extends AutoCompleteTextView {
111111
private int errorColor;
112112

113113
/**
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.
115120
*/
116121
private int maxCharacters;
117122

@@ -260,6 +265,7 @@ public MaterialAutoCompleteTextView(Context context, AttributeSet attrs, int sty
260265
primaryColor = typedArray.getColor(R.styleable.MaterialEditText_primaryColor, defaultPrimaryColor);
261266
setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_floatingLabel, 0));
262267
errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, Color.parseColor("#e7492E"));
268+
minCharacters = typedArray.getInt(R.styleable.MaterialEditText_minCharacters, 0);
263269
maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0);
264270
singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false);
265271
helperText = typedArray.getString(R.styleable.MaterialEditText_helperText);
@@ -421,7 +427,7 @@ private void initPadding() {
421427
* calculate {@link #minBottomLines}
422428
*/
423429
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;
425431
currentBottomLines = minBottomLines = minBottomTextLines > 0 ? minBottomTextLines : extendBottom ? 1 : 0;
426432
}
427433

@@ -603,6 +609,17 @@ public void setMaxCharacters(int max) {
603609
postInvalidate();
604610
}
605611

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+
606623
public int getErrorColor() {
607624
return errorColor;
608625
}
@@ -647,7 +664,7 @@ public CharSequence getError() {
647664
* only used to draw the bottom line
648665
*/
649666
private boolean isInternalValid() {
650-
return tempErrorText == null && isMaxCharactersValid();
667+
return tempErrorText == null && isCharactersCountValid();
651668
}
652669

653670
/**
@@ -811,9 +828,17 @@ protected void onDraw(@NonNull Canvas canvas) {
811828
float fontPaddingTop = floatingLabelTextSize + fontMetrics.ascent + fontMetrics.descent;
812829

813830
// 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+
817842
canvas.drawText(text, getWidth() + getScrollX() - textPaint.measureText(text), lineStartY + bottomSpacing + relativeHeight, textPaint);
818843
}
819844

@@ -871,11 +896,15 @@ private int getBottomTextLeftOffset() {
871896
}
872897

873898
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));
875904
}
876905

877-
public boolean isMaxCharactersValid() {
878-
return maxCharacters <= 0 || getText() == null || getText().length() <= maxCharacters;
906+
private boolean hasCharatersCounter() {
907+
return minCharacters > 0 || maxCharacters > 0;
879908
}
880909

881910
@Override

library/src/main/java/com/rengwuxian/materialedittext/MaterialEditText.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ public class MaterialEditText extends EditText {
111111
private int errorColor;
112112

113113
/**
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.
115120
*/
116121
private int maxCharacters;
117122

@@ -260,6 +265,7 @@ public MaterialEditText(Context context, AttributeSet attrs, int style) {
260265
primaryColor = typedArray.getColor(R.styleable.MaterialEditText_primaryColor, defaultPrimaryColor);
261266
setFloatingLabelInternal(typedArray.getInt(R.styleable.MaterialEditText_floatingLabel, 0));
262267
errorColor = typedArray.getColor(R.styleable.MaterialEditText_errorColor, Color.parseColor("#e7492E"));
268+
minCharacters = typedArray.getInt(R.styleable.MaterialEditText_minCharacters, 0);
263269
maxCharacters = typedArray.getInt(R.styleable.MaterialEditText_maxCharacters, 0);
264270
singleLineEllipsis = typedArray.getBoolean(R.styleable.MaterialEditText_singleLineEllipsis, false);
265271
helperText = typedArray.getString(R.styleable.MaterialEditText_helperText);
@@ -421,7 +427,7 @@ private void initPadding() {
421427
* calculate {@link #minBottomLines}
422428
*/
423429
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;
425431
currentBottomLines = minBottomLines = minBottomTextLines > 0 ? minBottomTextLines : extendBottom ? 1 : 0;
426432
}
427433

@@ -603,6 +609,17 @@ public void setMaxCharacters(int max) {
603609
postInvalidate();
604610
}
605611

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+
606623
public int getErrorColor() {
607624
return errorColor;
608625
}
@@ -647,7 +664,7 @@ public CharSequence getError() {
647664
* only used to draw the bottom line
648665
*/
649666
private boolean isInternalValid() {
650-
return tempErrorText == null && isMaxCharactersValid();
667+
return tempErrorText == null && isCharactersCountValid();
651668
}
652669

653670
/**
@@ -811,9 +828,17 @@ protected void onDraw(@NonNull Canvas canvas) {
811828
float fontPaddingTop = floatingLabelTextSize + fontMetrics.ascent + fontMetrics.descent;
812829

813830
// 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+
817842
canvas.drawText(text, getWidth() + getScrollX() - textPaint.measureText(text), lineStartY + bottomSpacing + relativeHeight, textPaint);
818843
}
819844

@@ -871,11 +896,15 @@ private int getBottomTextLeftOffset() {
871896
}
872897

873898
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));
875904
}
876905

877-
public boolean isMaxCharactersValid() {
878-
return maxCharacters <= 0 || getText() == null || getText().length() <= maxCharacters;
906+
private boolean hasCharatersCounter() {
907+
return minCharacters > 0 || maxCharacters > 0;
879908
}
880909

881910
@Override

library/src/main/res/values/attrs.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
</attr>
1414
<!-- The color for when something is wrong.(e.g. exceeding max characters) -->
1515
<attr name="errorColor" format="color"/>
16-
<!-- Characters count limit. 0 means no limit. -->
16+
<!-- Min characters count limit. 0 means no limit. -->
17+
<attr name="minCharacters" format="integer"/>
18+
<!-- max Characters count limit. 0 means no limit. -->
1719
<attr name="maxCharacters" format="integer"/>
1820
<!-- Whether to show the bottom ellipsis in singleLine mode -->
1921
<attr name="singleLineEllipsis" format="boolean"/>

sample/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@
234234
android:layout_width="match_parent"
235235
android:layout_height="wrap_content"
236236
android:hint="Max Characters"
237-
app:maxCharacters="10"/>
237+
app:minCharacters="8"/>
238238

239239
<TextView
240240
android:layout_width="match_parent"

0 commit comments

Comments
 (0)