Skip to content

Commit

Permalink
[TextInputLayout] Add support for minEms and maxEms
Browse files Browse the repository at this point in the history
Resolves #2269

PiperOrigin-RevId: 423118234
  • Loading branch information
paulfthomas authored and dsn5ft committed Jan 24, 2022
1 parent 285fa9d commit 7d46b95
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
6 changes: 3 additions & 3 deletions docs/components/TextField.md
Expand Up @@ -319,9 +319,9 @@ The recommended default `android:layout_width` is `245dp`.

By default, text fields have a maximum width of `488dp`, and a minimum width of
`56dp` for layouts without a label. If a label is present, the minimum width
recommended is `88dp`. `android:minWidth` and `android:maxWidth` should be set
on the `TextInputLayout` instead of on the `TextInputEditText` to avoid
unintended behaviors.
recommended is `88dp`. `android:minWidth` and `android:maxWidth` (as well as
`android:minEms` and `android:maxEms`) should be set on the `TextInputLayout`
instead of on the `TextInputEditText` to avoid unintended behaviors.

You can override those values in a custom style that inherits from a
`TextInputLayout` style or by making changes directly on the layout:
Expand Down
Expand Up @@ -200,6 +200,8 @@ public class TextInputLayout extends LinearLayout {
EditText editText;
private CharSequence originalHint;

private int minEms = NO_WIDTH;
private int maxEms = NO_WIDTH;
private int minWidth = NO_WIDTH;
private int maxWidth = NO_WIDTH;

Expand Down Expand Up @@ -507,10 +509,14 @@ public TextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs, i
hintAnimationEnabled = a.getBoolean(R.styleable.TextInputLayout_hintAnimationEnabled, true);
expandedHintEnabled = a.getBoolean(R.styleable.TextInputLayout_expandedHintEnabled, true);

if (a.hasValue(R.styleable.TextInputLayout_android_minWidth)) {
if (a.hasValue(R.styleable.TextInputLayout_android_minEms)) {
setMinEms(a.getInt(R.styleable.TextInputLayout_android_minEms, NO_WIDTH));
} else if (a.hasValue(R.styleable.TextInputLayout_android_minWidth)) {
setMinWidth(a.getDimensionPixelSize(R.styleable.TextInputLayout_android_minWidth, NO_WIDTH));
}
if (a.hasValue(R.styleable.TextInputLayout_android_maxWidth)) {
if (a.hasValue(R.styleable.TextInputLayout_android_maxEms)) {
setMaxEms(a.getInt(R.styleable.TextInputLayout_android_maxEms, NO_WIDTH));
} else if (a.hasValue(R.styleable.TextInputLayout_android_maxWidth)) {
setMaxWidth(a.getDimensionPixelSize(R.styleable.TextInputLayout_android_maxWidth, NO_WIDTH));
}

Expand Down Expand Up @@ -1458,8 +1464,16 @@ private void setEditText(EditText editText) {
}

this.editText = editText;
setMinWidth(minWidth);
setMaxWidth(maxWidth);
if (minEms != NO_WIDTH) {
setMinEms(minEms);
} else {
setMinWidth(minWidth);
}
if (maxEms != NO_WIDTH) {
setMaxEms(maxEms);
} else {
setMaxWidth(maxWidth);
}
onApplyBoxBackgroundMode();
setTextInputAccessibilityDelegate(new AccessibilityDelegate(this));

Expand Down Expand Up @@ -1613,6 +1627,56 @@ public EditText getEditText() {
return editText;
}

/**
* Sets the minimum width in terms of ems of the text field. The layout will be at least
* {@code minEms} wide if its {@code layout_width} is set to {@code wrap_content}.
*
* @param minEms The minimum width in terms of ems to be set
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_minEms
* @see #getMinEms()
*/
public void setMinEms(int minEms) {
this.minEms = minEms;
if (editText != null && minEms != NO_WIDTH) {
editText.setMinEms(minEms);
}
}

/**
* Returns the text field's minimum width in terms of ems, or -1 if no minimum width is set.
*
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_minEms
* @see #setMinEms(int)
*/
public int getMinEms() {
return minEms;
}

/**
* Sets the maximum width in terms of ems of the text field. The layout will be at most
* {@code maxEms} wide if its {@code layout_width} is set to {@code wrap_content}.
*
* @param maxEms The maximum width in terms of ems to be set
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_maxEms
* @see #getMaxEms()
*/
public void setMaxEms(int maxEms) {
this.maxEms = maxEms;
if (editText != null && maxEms != NO_WIDTH) {
editText.setMaxEms(maxEms);
}
}

/**
* Returns the text field's maximum width in terms of ems, or -1 if no maximum width is set.
*
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_maxEms
* @see #setMaxEms(int)
*/
public int getMaxEms() {
return maxEms;
}

/**
* Sets the minimum width of the text field. The layout will be at least this dimension wide if
* its {@code layout_width} is set to {@code wrap_content}.
Expand Down Expand Up @@ -1688,7 +1752,7 @@ public void setMaxWidthResource(@DimenRes int maxWidthId) {
*
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_maxWidth
* @see #setMaxWidth(int)
* @see #setMaxWidthResource(int) (int)
* @see #setMaxWidthResource(int)
*/
@Px
public int getMaxWidth() {
Expand Down
Expand Up @@ -45,11 +45,17 @@
<!-- The text color for the hint when the text field is not activated (such
as for the resting and disabled states). -->
<attr name="android:textColorHint"/>
<!-- Makes the text field be at least this dimension wide if its width is
<!-- Makes the text field be at least this many ems wide if its width is
set to wrap_content. -->
<attr name="android:minEms"/>
<!-- Makes the text field be at most this many ems wide if its width is set
to wrap_content. -->
<attr name="android:maxEms"/>
<!-- Makes the text field be at least this dimension wide in pixels if its width is
set to wrap_content. -->
<attr name="android:minWidth"/>
<!-- Makes the text field be at most this dimension wide if its width is set
to wrap_content. -->
<!-- Makes the text field be at most this dimension wide in pixels if its width is set
to wrap_content. -->
<attr name="android:maxWidth"/>

<!-- Whether the layout's floating label functionality is enabled. -->
Expand Down Expand Up @@ -238,7 +244,7 @@
<attr name="boxStrokeErrorColor" format="color"/>
<!-- The color to use for the box's background color when in filled box mode.
If a non-stateful color resource is specified, default colors will be used for the hover
and disabled states. -->
and disabled states. -->
<attr name="boxBackgroundColor" format="color"/>
<!-- The value to use for the box's stroke when in outline box mode, or for the underline stroke
in filled mode. -->
Expand Down

0 comments on commit 7d46b95

Please sign in to comment.