Skip to content

Commit f97560d

Browse files
leticiarossiwcshi
authored andcommitted
[TextInputLayout][Large Screens] Adding setMinWidth and setMaxWidth methods on the TextInputLayout so it works as expected.
PiperOrigin-RevId: 366041873
1 parent 740a3d2 commit f97560d

File tree

4 files changed

+131
-4
lines changed

4 files changed

+131
-4
lines changed

docs/components/TextField.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,36 @@ _**Note:** Non-null error text will replace any existing helper text._
312312

313313
### Text field dimensions
314314

315-
By default, text fields have a maximum width of `488dp`, and a minimum with of
315+
The recommended default `android:layout_width` is `245dp`.
316+
317+
By default, text fields have a maximum width of `488dp`, and a minimum width of
316318
`56dp` for layouts without a label. If a label is present, the minimum width
317-
recommended is of `88dp`.
319+
recommended is `88dp`. `android:minWidth` and `android:maxWidth` should be set
320+
on the `TextInputLayout` instead of on the `TextInputEditText` to avoid
321+
unintended behaviors.
322+
323+
You can override those values in a custom style that inherits from a
324+
`TextInputLayout` style or directly on the layout:
325+
326+
```xml
327+
<com.google.android.material.textfield.TextInputLayout
328+
android:id="@+id/textField"
329+
android:layout_width="wrap_content"
330+
android:layout_height="wrap_content"
331+
android:minWidth="@dimen/custom_min_width"
332+
android:maxWidth="@dimen/custom_max_width"
333+
android:hint="@string/label">
334+
335+
<com.google.android.material.textfield.TextInputEditText
336+
android:layout_width="match_parent"
337+
android:layout_height="wrap_content"
338+
/>
339+
340+
</com.google.android.material.textfield.TextInputLayout>
341+
```
318342

319-
The recommended default `android:layout_width` is of `245dp`.
343+
_**Note:** The `android:layout_width` of the `TextInputLayout` should be
344+
`wrap_content` in order for those minimum and maximum dimensions to be used._
320345

321346
### Using text fields programmatically
322347

lib/java/com/google/android/material/textfield/TextInputLayout.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import androidx.annotation.IntDef;
7474
import androidx.annotation.NonNull;
7575
import androidx.annotation.Nullable;
76+
import androidx.annotation.Px;
7677
import androidx.annotation.RestrictTo;
7778
import androidx.annotation.StringRes;
7879
import androidx.annotation.StyleRes;
@@ -183,6 +184,7 @@ public class TextInputLayout extends LinearLayout {
183184
private static final int LABEL_SCALE_ANIMATION_DURATION = 167;
184185

185186
private static final int INVALID_MAX_LENGTH = -1;
187+
private static final int NO_WIDTH = -1;
186188

187189
private static final String LOG_TAG = "TextInputLayout";
188190

@@ -193,6 +195,9 @@ public class TextInputLayout extends LinearLayout {
193195
EditText editText;
194196
private CharSequence originalHint;
195197

198+
private int minWidth = NO_WIDTH;
199+
private int maxWidth = NO_WIDTH;
200+
196201
private final IndicatorViewController indicatorViewController = new IndicatorViewController(this);
197202

198203
boolean counterEnabled;
@@ -478,6 +483,13 @@ public TextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs, i
478483
hintAnimationEnabled = a.getBoolean(R.styleable.TextInputLayout_hintAnimationEnabled, true);
479484
expandedHintEnabled = a.getBoolean(R.styleable.TextInputLayout_expandedHintEnabled, true);
480485

486+
if (a.hasValue(R.styleable.TextInputLayout_android_minWidth)) {
487+
setMinWidth(a.getDimensionPixelSize(R.styleable.TextInputLayout_android_minWidth, NO_WIDTH));
488+
}
489+
if (a.hasValue(R.styleable.TextInputLayout_android_maxWidth)) {
490+
setMaxWidth(a.getDimensionPixelSize(R.styleable.TextInputLayout_android_maxWidth, NO_WIDTH));
491+
}
492+
481493
shapeAppearanceModel =
482494
ShapeAppearanceModel.builder(context, attrs, defStyleAttr, DEF_STYLE_RES).build();
483495

@@ -1388,6 +1400,8 @@ private void setEditText(EditText editText) {
13881400
}
13891401

13901402
this.editText = editText;
1403+
setMinWidth(minWidth);
1404+
setMaxWidth(maxWidth);
13911405
onApplyBoxBackgroundMode();
13921406
setTextInputAccessibilityDelegate(new AccessibilityDelegate(this));
13931407

@@ -1538,6 +1552,88 @@ public EditText getEditText() {
15381552
return editText;
15391553
}
15401554

1555+
/**
1556+
* Sets the minimum width of the text field. The layout will be at least this dimension wide if
1557+
* its {@code layout_width} is set to {@code wrap_content}.
1558+
*
1559+
* @param minWidth The minimum width to be set
1560+
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_minWidth
1561+
* @see #setMinWidthResource(int)
1562+
* @see #getMinWidth()
1563+
*/
1564+
public void setMinWidth(@Px int minWidth) {
1565+
this.minWidth = minWidth;
1566+
if (editText != null && minWidth != NO_WIDTH) {
1567+
editText.setMinWidth(minWidth);
1568+
}
1569+
}
1570+
1571+
/**
1572+
* Sets the minimum width of the text field. The layout will be at least this dimension wide if
1573+
* its {@code layout_width} is set to {@code wrap_content}.
1574+
*
1575+
* @param minWidthId The id of the minimum width dimension resource to be set
1576+
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_minWidth
1577+
* @see #setMinWidth(int)
1578+
* @see #getMinWidth()
1579+
*/
1580+
public void setMinWidthResource(@DimenRes int minWidthId) {
1581+
setMinWidth(getContext().getResources().getDimensionPixelSize(minWidthId));
1582+
}
1583+
1584+
/**
1585+
* Returns the text field's minimum width, or -1 if no minimum width is set.
1586+
*
1587+
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_minWidth
1588+
* @see #setMinWidth(int)
1589+
* @see #setMinWidthResource(int) (int)
1590+
*/
1591+
@Px
1592+
public int getMinWidth() {
1593+
return minWidth;
1594+
}
1595+
1596+
/**
1597+
* Sets the maximum width of the text field. The layout will be at most this dimension wide if
1598+
* its {@code layout_width} is set to {@code wrap_content}.
1599+
*
1600+
* @param maxWidth The maximum width to be set
1601+
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_maxWidth
1602+
* @see #setMaxWidthResource(int)
1603+
* @see #getMaxWidth()
1604+
*/
1605+
public void setMaxWidth(@Px int maxWidth) {
1606+
this.maxWidth = maxWidth;
1607+
if (editText != null && maxWidth != NO_WIDTH) {
1608+
editText.setMaxWidth(maxWidth);
1609+
}
1610+
}
1611+
1612+
/**
1613+
* Sets the maximum width of the text field. The layout will be at most this dimension wide if
1614+
* its {@code layout_width} is set to {@code wrap_content}.
1615+
*
1616+
* @param maxWidthId The id of the maximum width dimension resource to be set
1617+
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_maxWidth
1618+
* @see #setMaxWidth(int)
1619+
* @see #getMaxWidth()
1620+
*/
1621+
public void setMaxWidthResource(@DimenRes int maxWidthId) {
1622+
setMaxWidth(getContext().getResources().getDimensionPixelSize(maxWidthId));
1623+
}
1624+
1625+
/**
1626+
* Returns the text field's maximum width, or -1 if no maximum width is set.
1627+
*
1628+
* @attr ref com.google.android.material.R.styleable#TextInputLayout_android_maxWidth
1629+
* @see #setMaxWidth(int)
1630+
* @see #setMaxWidthResource(int) (int)
1631+
*/
1632+
@Px
1633+
public int getMaxWidth() {
1634+
return maxWidth;
1635+
}
1636+
15411637
/**
15421638
* Set the hint to be displayed in the floating label, if enabled.
15431639
*

lib/java/com/google/android/material/textfield/res/values/attrs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<!-- The text color for the hint when the text field is not activated (such
2828
as for the resting and disabled states). -->
2929
<attr name="android:textColorHint"/>
30+
<!-- Makes the text field be at least this dimension wide if its width is
31+
set to wrap_content. -->
32+
<attr name="android:minWidth"/>
33+
<!-- Makes the text field be at most this dimension wide if its width is set
34+
to wrap_content. -->
35+
<attr name="android:maxWidth"/>
3036

3137
<!-- Whether the layout's floating label functionality is enabled. -->
3238
<attr name="hintEnabled" format="boolean"/>

lib/java/com/google/android/material/textfield/res/values/styles.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<item name="enforceTextAppearance">true</item>
5959

6060
<item name="android:minWidth">@dimen/material_textinput_min_width</item>
61-
<item name="android:maxWidth">@dimen/material_textinput_min_width</item>
61+
<item name="android:maxWidth">@dimen/material_textinput_max_width</item>
6262
<item name="boxBackgroundMode">outline</item>
6363
<item name="boxBackgroundColor">@null</item>
6464
<item name="errorIconDrawable">@drawable/mtrl_ic_error</item>

0 commit comments

Comments
 (0)