7373import androidx .annotation .IntDef ;
7474import androidx .annotation .NonNull ;
7575import androidx .annotation .Nullable ;
76+ import androidx .annotation .Px ;
7677import androidx .annotation .RestrictTo ;
7778import androidx .annotation .StringRes ;
7879import 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 *
0 commit comments