Skip to content

Commit ef919e8

Browse files
afohrmanikim24
authored andcommitted
Use EditText's background for the filled text field and fix underline flakiness.
Use the EditText's background for the filled box text field and a MaterialShapeDrawable drawn to the canvas for the underline. This allows for many improvements to the filled text field. This commit: - Fixes the underline flakiness, where the underline used to not meet the edges until drawableStateChanged() was called after a layout pass (because TextInputLayout was attempting to use EditText's pre-existing underline, and the bounds of that Drawable weren't known in order to modify them until after drawableStateChanged()) - Removes the hard-coded box offset since it was only necessary when trying to use EditText's background drawable - Brings the expanded label/hint a bit closer to the vertical center. Any more than this will require changing the bounds set on the expanded hint text and create misalignment between the expanded hint and any compound drawable that might be set Specs: https://material.io/design/components/text-fields.html#spec There is a possible alternative to drawing the underline manually: including the underline as part of the EditText's background. I investigated this option and rejected it, since it would require a re-architecture of the way TextInputLayout handles the box logic and many layers of box drawable nesting. It is much more straightforward and performant to draw the underline directly to the canvas than to convert the boxBackground to {LayerDrawable{MaterialShapeDrawable,{InsetDrawable{MaterialShapeDrawable}}}}. This commit should only change the visuals of the filled text field; the outline text field should be visually untouched. PiperOrigin-RevId: 231246920
1 parent 9c4bed6 commit ef919e8

File tree

6 files changed

+193
-184
lines changed

6 files changed

+193
-184
lines changed

docs/components/TextInputLayout.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
7575
To change the background color of a filled text field, you can set the
7676
`boxBackgroundColor` attribute on your `TextInputLayout`.
7777

78+
Note: When using a filled text field with an `EditText` child that is not a
79+
`TextInputEditText`, make sure to set the `EditText`'s `android:background` to
80+
`@null`. This allows `TextInputLayout` to set a filled background on the
81+
`EditText`.
82+
7883
#### Outlined Box
7984

8085
Outlined text fields have a stroked border and are less emphasized. To use an
@@ -88,7 +93,7 @@ To change the stroke color and width for an outline text field, you can set the
8893
`boxStrokeColor` and `boxStrokeWidth` attributes on your `TextInputLayout`,
8994
respectively.
9095

91-
When using an outlined text field with an `EditText` child that is not a
96+
Note: When using an outlined text field with an `EditText` child that is not a
9297
`TextInputEditText`, make sure to set the `EditText`'s `android:background` to
9398
`@null`. This allows `TextInputLayout` to set an outline background on the
9499
`EditText`.

0 commit comments

Comments
 (0)