Text Input Layout
Layout which wraps a TextInputEditText, android.widget.EditText, or descendant to show a floating label when the hint is hidden while the user inputs text.
Also supports:
- Showing an error via setErrorEnabled and setError, along with showing an error icon via setErrorIconDrawable
- Showing helper text via setHelperTextEnabled and setHelperText
- Showing placeholder text via setPlaceholderText
- Showing prefix text via setPrefixText
- Showing suffix text via setSuffixText
- Showing a character counter via setCounterEnabled and setCounterMaxLength
- Password visibility toggling via setEndIconMode API and related attribute. If set, a button is displayed to toggle between the password being displayed as plain-text or disguised, when your EditText is set to display a password.
- Clearing text functionality via setEndIconMode API and related attribute. If set, a button is displayed when text is present and clicking it clears the EditText field.
- Showing a custom icon specified via setEndIconMode API and related attribute. You should specify a drawable and content description for the icon. Optionally, you can also specify an android.view.View.OnClickListener, an and an OnEndIconChangedListener.
Note: When using an end icon, the 'end' compound drawable of the EditText will be overridden while the end icon view is visible. To ensure that any existing drawables are restored correctly, you should set those compound drawables relatively (start/end), as opposed to absolutely (left/right).
- Showing a start icon via setStartIconDrawable API and related attribute. You should specify a content description for the icon. Optionally, you can also specify an android.view.View.OnClickListener for it.
Note: Use the setStartIconDrawable API in place of setting a start/left compound drawable on the EditText. When using a start icon, the 'start/left' compound drawable of the EditText will be overridden.
- Showing a button that when clicked displays a dropdown menu. The selected option is displayed above the dropdown. You need to use an AutoCompleteTextView instead of a TextInputEditText as the input text child, and a Widget.MaterialComponents.TextInputLayout.(...).ExposedDropdownMenu style.
To disable user input you should set
on the AutoCompleteTextView.android:editable="false"
Content copied to clipboard
The TextInputEditText class is provided to be used as the input text child of this layout. Using TextInputEditText instead of an EditText provides accessibility support for the text field and allows TextInputLayout greater control over the visual aspects of the text field. This is an example usage:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/form_username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
If you construct the TextInputEditText child of a TextInputLayout programmatically, you should use TextInputLayout's context
to create the view. This will allow TextInputLayout to pass along the appropriate styling to the .
If the EditText child is not a TextInputEditText, make sure to set the 's android:background
to null
when using an outlined or filled text field. This allows TextInputLayout to set the EditText's background to an outlined or filled box, respectively.
Note: The actual view hierarchy present under TextInputLayout is NOT guaranteed to match the view hierarchy as written in XML. As a result, calls to getParent()
on children of the TextInputLayout -- such as a TextInputEditText -- may not return the TextInputLayout itself, but rather an intermediate View. If you need to access a View directly, set an android:id
and use findViewById.
For more information, see the component developer guidance and design guidelines.
Types
Properties
Widget.MaterialComponents.TextInputLayout.(...).ExposedDropdownMenu
style is being used.true
when providing a hint on behalf of a child EditText.Functions
null
if no error was set or if error displaying is not enabled.null
if no helper text was set or if helper text functionality is not enabled.null
if there is no prefix text.null
if there is no suffix text.