TextInputLayout

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

    android:editable="false"
    on the AutoCompleteTextView.

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>
The hint should be set on the TextInputLayout, rather than the EditText. If a hint is specified on the child EditText in XML, the TextInputLayout might still work correctly; TextInputLayout will use the EditText's hint as its floating label. However, future calls to modify the hint will not update TextInputLayout's hint. To avoid unintended behavior, call setHint and getHint on TextInputLayout, instead of on EditText.

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.

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)

Types

Link copied to clipboard
An AccessibilityDelegate intended to be set on an EditText or with setTextInputAccessibilityDelegate to provide attributes for accessibility that are managed by TextInputLayout.
Link copied to clipboard
Values for box background mode.
Link copied to clipboard
Values for the end icon mode.
Link copied to clipboard
interface LengthCounter
Interface definition for a length counter.
Link copied to clipboard
Callback interface invoked when the view's EditText is attached, or from addOnEditTextAttachedListener if the edit text is already present.
Link copied to clipboard
Callback interface invoked when the view's end icon changes.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open val editText: EditText
Link copied to clipboard
The TextInputLayout will show a clear text button while there is input in the EditText.
Link copied to clipboard
The TextInputLayout will show a custom icon specified by the user.
Link copied to clipboard
The TextInputLayout will show a dropdown button if the EditText is an and a Widget.MaterialComponents.TextInputLayout.(...).ExposedDropdownMenu style is being used.
Link copied to clipboard
Default for the TextInputLayout.
Link copied to clipboard
The TextInputLayout will show a password toggle button if its EditText displays a password.
Link copied to clipboard
open var hint: CharSequence
Link copied to clipboard
true when providing a hint on behalf of a child EditText.
Link copied to clipboard
open var maxEms: Int
Link copied to clipboard
@get:Px
open var maxWidth: Int
Link copied to clipboard
open var minEms: Int
Link copied to clipboard
@get:Px
open var minWidth: Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open var typeface: Typeface

Functions

Link copied to clipboard
Add a OnEditTextAttachedListener that will be invoked when the edit text is attached, or from this method if the EditText is already present.
Link copied to clipboard
Add a TextInputLayout.OnEndIconChangedListener that will be invoked when the end icon gets changed.
Link copied to clipboard
open fun addView(@NonNull child: View, index: Int, @NonNull params: ViewGroup.LayoutParams)
Link copied to clipboard
Remove all previously added OnEditTextAttachedListeners.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun draw(@NonNull canvas: Canvas)
Link copied to clipboard
open fun getBaseline(): Int
Link copied to clipboard
Returns the EditText's collapsed top padding
Link copied to clipboard
Returns the box's bottom end corner radius.
Link copied to clipboard
Returns the box's bottom start corner radius.
Link copied to clipboard
Returns the box's top end corner radius.
Link copied to clipboard
Returns the box's top start corner radius.
Link copied to clipboard
Returns the box's stroke color when an error is being displayed.
Link copied to clipboard
Returns the box's stroke width.
Link copied to clipboard
Returns the box's stroke focused width.
Link copied to clipboard
Returns the currently configured content description for the end icon.
Link copied to clipboard
Returns the drawable currently used for the end icon.
Link copied to clipboard
Returns the minimum size of the end icon.
Link copied to clipboard
open fun getEndIconMode(): Int
Returns the current end icon mode.
Link copied to clipboard
Returns the ImageView.
Link copied to clipboard
Returns the error message that was set to be displayed with setError, or null if no error was set or if error displaying is not enabled.
Link copied to clipboard
Returns the accessibility live region of the error message.
Link copied to clipboard
Returns the content description of the error message, or null if not set.
Link copied to clipboard
Returns the text color used by the error message in current state.
Link copied to clipboard
Returns the drawable currently used for the error icon.
Link copied to clipboard
Returns the helper message that was set to be displayed with setHelperText, or null if no helper text was set or if helper text functionality is not enabled.
Link copied to clipboard
Returns the text color used by the helper text in the current states.
Link copied to clipboard
Gets the collapsed hint text color.
Returns the currently configured content description for the password visibility toggle button.
Link copied to clipboard
Returns the icon currently used for the password visibility toggle button.
Link copied to clipboard
Returns the prefix text that was set to be displayed with setPrefixText, or null if there is no prefix text.
Link copied to clipboard
Returns the ColorStateList used for the prefix text.
Link copied to clipboard
Returns the prefix text view.
Link copied to clipboard
Returns the currently configured content description for the start icon.
Link copied to clipboard
Returns the start icon.
Link copied to clipboard
Returns the size of the start icon.
Link copied to clipboard
Returns the ImageView.
Link copied to clipboard
Returns the suffix text that was set to be displayed with setSuffixText, or null if there is no suffix text.
Link copied to clipboard
Returns the ColorStateList used for the suffix text.
Link copied to clipboard
Returns the suffix text view.
Link copied to clipboard
Returns whether the character counter functionality is enabled or not in this layout.
Link copied to clipboard
Returns whether the end icon is checkable.
Link copied to clipboard
Returns whether the current end icon is visible.
Link copied to clipboard
Returns whether the error functionality is enabled or not in this layout.
Link copied to clipboard
Returns whether the hint expands to occupy the input area when the text field is unpopulated and not focused.
Link copied to clipboard
Returns whether the helper text functionality is enabled or not in this layout.
Link copied to clipboard
Returns whether any hint state changes, due to being focused or non-empty text, are animated.
Link copied to clipboard
Returns whether the floating label functionality is enabled or not in this layout.
Link copied to clipboard
Returns whether the password visibility toggle functionality is currently enabled.
Link copied to clipboard
Returns whether the start icon is checkable.
Link copied to clipboard
Returns whether the current start icon is visible.
Link copied to clipboard
open fun onGlobalLayout()
Link copied to clipboard
open fun onRtlPropertiesChanged(layoutDirection: Int)
Link copied to clipboard
Link copied to clipboard
open fun passwordVisibilityToggleRequested(shouldSkipAnimations: Boolean)
Handles visibility for a password toggle icon when changing obfuscation in a password edit text.
Link copied to clipboard
This method should be called from within your icon's click listener if your icon's tint list has a color for a state that depends on a click (such as checked state).
Link copied to clipboard
This method should be called from within your icon's click listener if your icon's tint list has a color for a state that depends on a click (such as checked state).
Link copied to clipboard
This method should be called from within your icon's click listener if your icon's tint list has a color for a state that depends on a click (such as checked state).
Link copied to clipboard
open fun setBoxBackgroundColorResource(@ColorRes boxBackgroundColorId: Int)
Set the resource used for the filled box's background color.
Link copied to clipboard
open fun setBoxBackgroundColorStateList(@NonNull boxBackgroundColorStateList: ColorStateList)
Sets the box's background color state list.
Link copied to clipboard
open fun setBoxCollapsedPaddingTop(boxCollapsedPaddingTop: Int)
Set the value to use for the EditText's collapsed top padding in box mode.
Link copied to clipboard
open fun setBoxCornerFamily(cornerFamily: Int)
Sets the box's corner family for all corners of the text field.
Link copied to clipboard
open fun setBoxCornerRadii(boxCornerRadiusTopStart: Float, boxCornerRadiusTopEnd: Float, boxCornerRadiusBottomStart: Float, boxCornerRadiusBottomEnd: Float)
Set the box's corner radii.
Link copied to clipboard
open fun setBoxCornerRadiiResources(@DimenRes boxCornerRadiusTopStartId: Int, @DimenRes boxCornerRadiusTopEndId: Int, @DimenRes boxCornerRadiusBottomEndId: Int, @DimenRes boxCornerRadiusBottomStartId: Int)
Set the resources used for the box's corner radii.
Link copied to clipboard
open fun setBoxStrokeColorStateList(@NonNull boxStrokeColorStateList: ColorStateList)
Set the box's stroke color state list.
Link copied to clipboard
open fun setBoxStrokeErrorColor(@Nullable strokeErrorColor: ColorStateList)
Set the outline box's stroke color when an error is being displayed.
Link copied to clipboard
open fun setBoxStrokeWidth(boxStrokeWidth: Int)
Set the value to use for the box's stroke when in outline box mode, or for the underline stroke in filled mode.
Link copied to clipboard
open fun setBoxStrokeWidthFocused(boxStrokeWidthFocused: Int)
Set the value to use for the focused box's stroke when in outline box mode, or for the focused underline stroke in filled mode.
Link copied to clipboard
open fun setBoxStrokeWidthFocusedResource(@DimenRes boxStrokeWidthFocusedResId: Int)
Set the resource dimension to use for the focused box's stroke when in outline box mode, or for the focused underline stroke in filled mode.
Link copied to clipboard
open fun setBoxStrokeWidthResource(@DimenRes boxStrokeWidthResId: Int)
Set the resource dimension to use for the box's stroke when in outline box mode, or for the underline stroke in filled mode.
Link copied to clipboard
open fun setCounterEnabled(enabled: Boolean)
Whether the character counter functionality is enabled or not in this layout.
Link copied to clipboard
open fun setCounterOverflowTextAppearance(counterOverflowTextAppearance: Int)
Sets the text color and size for the overflowed character counter using the specified TextAppearance resource.
Link copied to clipboard
open fun setCounterTextAppearance(counterTextAppearance: Int)
Sets the text color and size for the character counter using the specified TextAppearance resource.
Link copied to clipboard
open fun setEnabled(enabled: Boolean)
Link copied to clipboard
open fun setEndIconActivated(endIconActivated: Boolean)
Sets the current end icon's state to be activated or not.
Link copied to clipboard
open fun setEndIconCheckable(endIconCheckable: Boolean)
Sets the current end icon to be checkable or not.
Link copied to clipboard
open fun setEndIconContentDescription(@Nullable endIconContentDescription: CharSequence)
Set a content description for the end icon.
Link copied to clipboard
open fun setEndIconDrawable(@Nullable endIconDrawable: Drawable)
Set the icon to use for the end icon.
Link copied to clipboard
open fun setEndIconMinSize(@IntRange(from = 0) iconSize: Int)
Sets the width and height of the end icon.
Link copied to clipboard
open fun setEndIconMode(endIconMode: Int)
Set up the end icon mode.
Link copied to clipboard
open fun setEndIconOnClickListener(@Nullable endIconOnClickListener: View.OnClickListener)
Sets the end icon's functionality that is performed when the icon is clicked.
Link copied to clipboard
open fun setEndIconOnLongClickListener(@Nullable endIconOnLongClickListener: View.OnLongClickListener)
Sets the end icon's functionality that is performed when the end icon is long clicked.
Link copied to clipboard
Sets ImageView.
Link copied to clipboard
open fun setEndIconTintList(@Nullable endIconTintList: ColorStateList)
Applies a tint to the end icon drawable.
Link copied to clipboard
open fun setEndIconTintMode(@Nullable endIconTintMode: PorterDuff.Mode)
Specifies the blending mode used to apply the tint specified by setEndIconTintList to the end icon drawable.
Link copied to clipboard
open fun setEndIconVisible(visible: Boolean)
Sets the current end icon to be VISIBLE or GONE.
Link copied to clipboard
open fun setError(@Nullable errorText: CharSequence)
Sets an error message that will be displayed below our EditText.
Link copied to clipboard
open fun setErrorAccessibilityLiveRegion(errorAccessibilityLiveRegion: Int)
Sets an accessibility live region for the error message.
Link copied to clipboard
open fun setErrorContentDescription(@Nullable errorContentDescription: CharSequence)
Sets a content description for the error message.
Link copied to clipboard
open fun setErrorEnabled(enabled: Boolean)
Whether the error functionality is enabled or not in this layout.
Link copied to clipboard
open fun setErrorIconDrawable(@Nullable errorIconDrawable: Drawable)
Set the drawable to use for the error icon.
Link copied to clipboard
open fun setErrorIconOnClickListener(@Nullable errorIconOnClickListener: View.OnClickListener)
Sets the error icon's functionality that is performed when the icon is clicked.
Link copied to clipboard
open fun setErrorIconOnLongClickListener(@Nullable errorIconOnLongClickListener: View.OnLongClickListener)
Sets the error icon's functionality that is performed when the end icon is long clicked.
Link copied to clipboard
open fun setErrorIconTintList(@Nullable errorIconTintList: ColorStateList)
Applies a tint to the error icon drawable.
Link copied to clipboard
open fun setErrorIconTintMode(@Nullable errorIconTintMode: PorterDuff.Mode)
Specifies the blending mode used to apply tint to the end icon drawable.
Link copied to clipboard
open fun setErrorTextAppearance(@StyleRes errorTextAppearance: Int)
Sets the text color and size for the error message from the specified TextAppearance resource.
Link copied to clipboard
open fun setErrorTextColor(@Nullable errorTextColor: ColorStateList)
Sets the text color used by the error message in all states.
Link copied to clipboard
open fun setExpandedHintEnabled(enabled: Boolean)
Sets whether the hint should expand to occupy the input area when the text field is unpopulated and not focused.
Link copied to clipboard
open fun setHelperText(@Nullable helperText: CharSequence)
Sets a helper message that will be displayed below the EditText.
Link copied to clipboard
open fun setHelperTextColor(@Nullable helperTextColor: ColorStateList)
Sets the text color used by the helper text in all states.
Link copied to clipboard
open fun setHelperTextEnabled(enabled: Boolean)
Whether the helper text functionality is enabled or not in this layout.
Link copied to clipboard
open fun setHelperTextTextAppearance(@StyleRes helperTextTextAppearance: Int)
Sets the text color and size for the helper text from the specified TextAppearance resource.
Link copied to clipboard
open fun setHint(@StringRes textHintId: Int)
Set the hint to be displayed in the floating label, if enabled, using the given resource id.
Link copied to clipboard
open fun setHintAnimationEnabled(enabled: Boolean)
Set whether any hint state changes, due to being focused or non-empty text, are animated.
Link copied to clipboard
open fun setHintEnabled(enabled: Boolean)
Sets whether the floating label functionality is enabled or not in this layout.
Link copied to clipboard
Sets the collapsed hint text color, size, style from the specified TextAppearance resource.
Link copied to clipboard
open fun setHintTextColor(@Nullable hintTextColor: ColorStateList)
Sets the collapsed hint text color from the specified ColorStateList resource.
Link copied to clipboard
open fun setMaxWidthResource(@DimenRes maxWidthId: Int)
Sets the maximum width of the text field.
Link copied to clipboard
open fun setMinWidthResource(@DimenRes minWidthId: Int)
Sets the minimum width of the text field.
Set a content description for the navigation button if one is present.
Link copied to clipboard
Set the icon to use for the password visibility toggle button.
Link copied to clipboard
Enables or disable the password visibility toggle functionality.
Link copied to clipboard
Applies a tint to the password visibility toggle drawable.
Link copied to clipboard
Specifies the blending mode used to apply the tint specified by setPasswordVisibilityToggleTintList to the password visibility toggle drawable.
Link copied to clipboard
open fun setPrefixText(@Nullable prefixText: CharSequence)
Sets prefix text that will be displayed in the input area when the hint is collapsed before text is entered.
Link copied to clipboard
open fun setPrefixTextAppearance(@StyleRes prefixTextAppearance: Int)
Sets the text color and size for the prefix text from the specified TextAppearance resource.
Link copied to clipboard
open fun setPrefixTextColor(@NonNull prefixTextColor: ColorStateList)
Sets the text color used by the prefix text in all states.
Link copied to clipboard
open fun setStartIconCheckable(startIconCheckable: Boolean)
Sets the current start icon to be checkable or not.
Link copied to clipboard
open fun setStartIconContentDescription(@Nullable startIconContentDescription: CharSequence)
Set a content description for the start icon.
Link copied to clipboard
open fun setStartIconDrawable(@Nullable startIconDrawable: Drawable)
Sets the start icon.
Link copied to clipboard
open fun setStartIconMinSize(@IntRange(from = 0) iconSize: Int)
Sets the width and height of the start icon.
Link copied to clipboard
open fun setStartIconOnClickListener(@Nullable startIconOnClickListener: View.OnClickListener)
Sets the start icon's functionality that is performed when the start icon is clicked.
Link copied to clipboard
open fun setStartIconOnLongClickListener(@Nullable startIconOnLongClickListener: View.OnLongClickListener)
Sets the start icon's functionality that is performed when the start icon is long clicked.
Link copied to clipboard
Sets ImageView.
Link copied to clipboard
open fun setStartIconTintList(@Nullable startIconTintList: ColorStateList)
Applies a tint to the start icon drawable.
Link copied to clipboard
open fun setStartIconTintMode(@Nullable startIconTintMode: PorterDuff.Mode)
Specifies the blending mode used to apply the tint specified by setEndIconTintList to the start icon drawable.
Link copied to clipboard
open fun setStartIconVisible(visible: Boolean)
Sets the start icon to be VISIBLE or GONE.
Link copied to clipboard
open fun setSuffixText(@Nullable suffixText: CharSequence)
Sets suffix text that will be displayed in the input area when the hint is collapsed before text is entered.
Link copied to clipboard
open fun setSuffixTextAppearance(@StyleRes suffixTextAppearance: Int)
Sets the text color and size for the suffix text from the specified TextAppearance resource.
Link copied to clipboard
open fun setSuffixTextColor(@NonNull suffixTextColor: ColorStateList)
Sets the text color used by the suffix text in all states.
Link copied to clipboard
Sets an TextInputLayout.AccessibilityDelegate providing an accessibility implementation for the EditText used by this layout.