Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

TextInputLayout

public class TextInputLayout
extends LinearLayout

java.lang.Object
   ↳ LinearLayout
     ↳ com.google.android.material.textfield.TextInputLayout


Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden while the user inputs text.

Also supports:

The TextInputEditText class is provided to be used as a child of this layout. Using TextInputEditText allows TextInputLayout greater control over the visual aspects of any text input. An example usage is as so:

 <com.google.android.material.textfield.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <com.google.android.material.textfield.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="@string/form_username"/>

 </com.google.android.material.textfield.TextInputLayout>
 

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 an 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 View.

Summary

Nested classes

@interface TextInputLayout.BoxBackgroundMode

Values for box background mode. 

Constants

int BOX_BACKGROUND_FILLED

int BOX_BACKGROUND_NONE

int BOX_BACKGROUND_OUTLINE

Public constructors

TextInputLayout(Context context)
TextInputLayout(Context context, AttributeSet attrs)
TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void addView(View child, int index, ViewGroup.LayoutParams params)
void dispatchProvideAutofillStructure(ViewStructure structure, int flags)
void draw(Canvas canvas)
int getBoxBackgroundColor()

Returns the box's background color.

int getBoxCollapsedPaddingBottom()

Get the box's collapsed bottom padding.

int getBoxCollapsedPaddingTop()

Get the box's collapsed top padding.

float getBoxCornerRadiusBottomLeft()

Returns the box's bottom left corner radius.

float getBoxCornerRadiusBottomRight()

Returns the box's bottom right corner radius.

float getBoxCornerRadiusTopLeft()

Returns the box's top left corner radius.

float getBoxCornerRadiusTopRight()

Returns the box's top right corner radius.

int getBoxExpandedPaddingBottom()

Get the box's expanded bottom padding.

int getBoxExpandedPaddingTop()

Get the box's expanded top padding.

int getBoxPaddingLeft()

Get the box's left padding.

int getBoxPaddingRight()

Get the box's right padding.

int getBoxStrokeColor()

Returns the box's stroke color.

int getCounterMaxLength()

Returns the max length shown at the character counter.

ColorStateList getDefaultHintTextColor()

Returns the text color used by the hint in both the collapsed and expanded states, or null if no color has been set.

EditText getEditText()

Returns the EditText used for text input.

CharSequence getError()

Returns the error message that was set to be displayed with setError(CharSequence), or null if no error was set or if error displaying is not enabled.

int getErrorCurrentTextColors()

Returns the text color used by the error message in current state.

CharSequence getHelperText()

Returns the helper message that was set to be displayed with setHelperText(CharSequence), or null if no helper text was set or if helper text functionality is not enabled.

int getHelperTextCurrentTextColor()

Returns the text color used by the helper text in the current states.

CharSequence getHint()

Returns the hint which is displayed in the floating label, if enabled.

CharSequence getPasswordVisibilityToggleContentDescription()

Returns the currently configured content description for the password visibility toggle button.

Drawable getPasswordVisibilityToggleDrawable()

Returns the icon currently used for the password visibility toggle button.

Typeface getTypeface()

Returns the typeface used for the hint and any label views (such as counter and error views).

boolean isCounterEnabled()

Returns whether the character counter functionality is enabled or not in this layout.

boolean isErrorEnabled()

Returns whether the error functionality is enabled or not in this layout.

boolean isHelperTextEnabled()

Returns whether the helper text functionality is enabled or not in this layout.

boolean isHintAnimationEnabled()

Returns whether any hint state changes, due to being focused or non-empty text, are animated.

boolean isHintEnabled()

Returns whether the floating label functionality is enabled or not in this layout.

boolean isPasswordVisibilityToggleEnabled()

Returns whether the password visibility toggle functionality is currently enabled.

Parcelable onSaveInstanceState()
void passwordVisibilityToggleRequested(boolean shouldSkipAnimations)

Handles visiblity for a password toggle icon when changing obfuscation in a password edit text.

void setBoxBackgroundColor(int boxBackgroundColor)

Set the filled box's background color.

void setBoxBackgroundColorResource(int boxBackgroundColorId)

Set the resource used for the filled box's background color.

void setBoxBackgroundMode(int boxBackgroundMode)

Set the mode for the box's background (filled, outline, or none).

void setBoxCornerRadii(float boxCornerRadiusTopLeft, float boxCornerRadiusTopRight, float boxCornerRadiusBottomLeft, float boxCornerRadiusBottomRight)

Set the box's corner radii.

void setBoxCornerRadiiResources(int boxCornerRadiusTopLeftId, int boxCornerRadiusTopRightId, int boxCornerRadiusBottomRightId, int boxCornerRadiusBottomLeftId)

Set the resources used for the box's corner radii.

void setBoxPadding(int boxPaddingLeft, int boxCollapsedPaddingTop, int boxExpandedPaddingTop, int boxPaddingRight, int boxCollapsedPaddingBottom, int boxExpandedPaddingBottom)

Set the box's padding.

void setBoxStrokeColor(int boxStrokeColor)

Set the outline box's stroke color.

void setCounterEnabled(boolean enabled)

Whether the character counter functionality is enabled or not in this layout.

void setCounterMaxLength(int maxLength)

Sets the max length to display at the character counter.

void setDefaultHintTextColor(ColorStateList textColor)

Sets the text color used by the hint in both the collapsed and expanded states.

void setEnabled(boolean enabled)
void setError(CharSequence errorText)

Sets an error message that will be displayed below our ERROR(/EditText).

void setErrorEnabled(boolean enabled)

Whether the error functionality is enabled or not in this layout.

void setErrorTextAppearance(int resId)

Sets the text color and size for the error message from the specified TextAppearance resource.

void setErrorTextColor(ColorStateList textColors)

Sets the text color used by the error message in all states.

void setHelperText(CharSequence helperText)

Sets a helper message that will be displayed below the ERROR(/EditText).

void setHelperTextColor(ColorStateList textColors)

Sets the text color used by the helper text in all states.

void setHelperTextEnabled(boolean enabled)

Whether the helper text functionality is enabled or not in this layout.

void setHelperTextTextAppearance(int resId)

Sets the text color and size for the helper text from the specified TextAppearance resource.

void setHint(CharSequence hint)

Set the hint to be displayed in the floating label, if enabled.

void setHintAnimationEnabled(boolean enabled)

Set whether any hint state changes, due to being focused or non-empty text, are animated.

void setHintEnabled(boolean enabled)

Sets whether the floating label functionality is enabled or not in this layout.

void setHintTextAppearance(int resId)

Sets the hint text color, size, style from the specified TextAppearance resource.

void setPasswordVisibilityToggleContentDescription(CharSequence description)

Set a content description for the navigation button if one is present.

void setPasswordVisibilityToggleContentDescription(int resId)

Set a content description for the navigation button if one is present.

void setPasswordVisibilityToggleDrawable(int resId)

Set the icon to use for the password visibility toggle button.

void setPasswordVisibilityToggleDrawable(Drawable icon)

Set the icon to use for the password visibility toggle button.

void setPasswordVisibilityToggleEnabled(boolean enabled)

Returns whether the password visibility toggle functionality is enabled or not.

void setPasswordVisibilityToggleTintList(ColorStateList tintList)

Applies a tint to the password visibility toggle drawable.

void setPasswordVisibilityToggleTintMode(PorterDuff.Mode mode)

Specifies the blending mode used to apply the tint specified by setPasswordVisibilityToggleTintList(ColorStateList) to the password visibility toggle drawable.

void setTypeface(Typeface typeface)

Set the typeface to use for the hint and any label views (such as counter and error views).

Protected methods

void dispatchRestoreInstanceState( container)
void drawableStateChanged()
void onLayout(boolean changed, int left, int top, int right, int bottom)
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void onRestoreInstanceState(Parcelable state)

Inherited methods

From class java.lang.Object

Constants

BOX_BACKGROUND_FILLED

int BOX_BACKGROUND_FILLED

Constant Value: 1 (0x00000001)

BOX_BACKGROUND_NONE

int BOX_BACKGROUND_NONE

Constant Value: 0 (0x00000000)

BOX_BACKGROUND_OUTLINE

int BOX_BACKGROUND_OUTLINE

Constant Value: 2 (0x00000002)

Public constructors

TextInputLayout

TextInputLayout (Context context)

Parameters
context Context

TextInputLayout

TextInputLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

TextInputLayout

TextInputLayout (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

Public methods

addView

void addView (View child, 
                int index, 
                ViewGroup.LayoutParams params)

Parameters
child View

index int

params ViewGroup.LayoutParams

dispatchProvideAutofillStructure

void dispatchProvideAutofillStructure (ViewStructure structure, 
                int flags)

Parameters
structure ViewStructure

flags int

draw

void draw (Canvas canvas)

Parameters
canvas Canvas

getBoxBackgroundColor

int getBoxBackgroundColor ()

Returns the box's background color.

Returns
int the color used for the box's background

getBoxCollapsedPaddingBottom

int getBoxCollapsedPaddingBottom ()

Get the box's collapsed bottom padding.

Returns
int the box's collapsed bottom padding in pixels

getBoxCollapsedPaddingTop

int getBoxCollapsedPaddingTop ()

Get the box's collapsed top padding.

Returns
int the box's collapsed top padding in pixels

getBoxCornerRadiusBottomLeft

float getBoxCornerRadiusBottomLeft ()

Returns the box's bottom left corner radius.

Returns
float the value used for the box's bottom left corner radius

getBoxCornerRadiusBottomRight

float getBoxCornerRadiusBottomRight ()

Returns the box's bottom right corner radius.

Returns
float the value used for the box's bottom right corner radius

getBoxCornerRadiusTopLeft

float getBoxCornerRadiusTopLeft ()

Returns the box's top left corner radius.

Returns
float the value used for the box's top left corner radius

getBoxCornerRadiusTopRight

float getBoxCornerRadiusTopRight ()

Returns the box's top right corner radius.

Returns
float the value used for the box's top right corner radius

getBoxExpandedPaddingBottom

int getBoxExpandedPaddingBottom ()

Get the box's expanded bottom padding.

Returns
int the box's expanded bottom padding in pixels

getBoxExpandedPaddingTop

int getBoxExpandedPaddingTop ()

Get the box's expanded top padding.

Returns
int the box's expanded top padding in pixels

getBoxPaddingLeft

int getBoxPaddingLeft ()

Get the box's left padding.

Returns
int the box's left padding in pixels

getBoxPaddingRight

int getBoxPaddingRight ()

Get the box's right padding.

Returns
int the box's right padding in pixels

getBoxStrokeColor

int getBoxStrokeColor ()

Returns the box's stroke color.

Returns
int the color used for the box's stroke

getCounterMaxLength

int getCounterMaxLength ()

Returns the max length shown at the character counter.

Returns
int

getDefaultHintTextColor

ColorStateList getDefaultHintTextColor ()

Returns the text color used by the hint in both the collapsed and expanded states, or null if no color has been set.

Returns
ColorStateList

getEditText

EditText getEditText ()

Returns the EditText used for text input.

Returns
EditText

getError

CharSequence getError ()

Returns the error message that was set to be displayed with setError(CharSequence), or null if no error was set or if error displaying is not enabled.

Returns
CharSequence

getErrorCurrentTextColors

int getErrorCurrentTextColors ()

Returns the text color used by the error message in current state.

Returns
int

getHelperText

CharSequence getHelperText ()

Returns the helper message that was set to be displayed with setHelperText(CharSequence), or null if no helper text was set or if helper text functionality is not enabled.

Returns
CharSequence

getHelperTextCurrentTextColor

int getHelperTextCurrentTextColor ()

Returns the text color used by the helper text in the current states.

Returns
int

getHint

CharSequence getHint ()

Returns the hint which is displayed in the floating label, if enabled.

Returns
CharSequence the hint, or null if there isn't one set, or the hint is not enabled.

getPasswordVisibilityToggleContentDescription

CharSequence getPasswordVisibilityToggleContentDescription ()

Returns the currently configured content description for the password visibility toggle button.

This will be used to describe the navigation action to users through mechanisms such as screen readers.

Returns
CharSequence

getPasswordVisibilityToggleDrawable

Drawable getPasswordVisibilityToggleDrawable ()

Returns the icon currently used for the password visibility toggle button.

Returns
Drawable

getTypeface

Typeface getTypeface ()

Returns the typeface used for the hint and any label views (such as counter and error views).

Returns
Typeface

isCounterEnabled

boolean isCounterEnabled ()

Returns whether the character counter functionality is enabled or not in this layout.

Returns
boolean

isErrorEnabled

boolean isErrorEnabled ()

Returns whether the error functionality is enabled or not in this layout.

Returns
boolean

isHelperTextEnabled

boolean isHelperTextEnabled ()

Returns whether the helper text functionality is enabled or not in this layout.

Returns
boolean

isHintAnimationEnabled

boolean isHintAnimationEnabled ()

Returns whether any hint state changes, due to being focused or non-empty text, are animated.

Returns
boolean

isHintEnabled

boolean isHintEnabled ()

Returns whether the floating label functionality is enabled or not in this layout.

Returns
boolean

isPasswordVisibilityToggleEnabled

boolean isPasswordVisibilityToggleEnabled ()

Returns whether the password visibility toggle functionality is currently enabled.

Returns
boolean

onSaveInstanceState

Parcelable onSaveInstanceState ()

Returns
Parcelable

passwordVisibilityToggleRequested

void passwordVisibilityToggleRequested (boolean shouldSkipAnimations)

Handles visiblity for a password toggle icon when changing obfuscation in a password edit text. Public so that clients can override this method for custom UI changes when toggling the display of password text

Parameters
shouldSkipAnimations boolean: true if the password toggle indicator icon should not animate changes

setBoxBackgroundColor

void setBoxBackgroundColor (int boxBackgroundColor)

Set the filled box's background color.

Parameters
boxBackgroundColor int: the color to use for the filled box's background

setBoxBackgroundColorResource

void setBoxBackgroundColorResource (int boxBackgroundColorId)

Set the resource used for the filled box's background color.

Parameters
boxBackgroundColorId int: the resource to use for the box's background color

setBoxBackgroundMode

void setBoxBackgroundMode (int boxBackgroundMode)

Set the mode for the box's background (filled, outline, or none).

Parameters
boxBackgroundMode int: the box's background mode.

setBoxCornerRadii

void setBoxCornerRadii (float boxCornerRadiusTopLeft, 
                float boxCornerRadiusTopRight, 
                float boxCornerRadiusBottomLeft, 
                float boxCornerRadiusBottomRight)

Set the box's corner radii.

Parameters
boxCornerRadiusTopLeft float: the value to use for the box's top left corner radius

boxCornerRadiusTopRight float: the value to use for the box's top right corner radius

boxCornerRadiusBottomLeft float: the value to use for the box's bottom left corner radius

boxCornerRadiusBottomRight float: the value to use for the box's bottom right corner radius

setBoxCornerRadiiResources

void setBoxCornerRadiiResources (int boxCornerRadiusTopLeftId, 
                int boxCornerRadiusTopRightId, 
                int boxCornerRadiusBottomRightId, 
                int boxCornerRadiusBottomLeftId)

Set the resources used for the box's corner radii.

Parameters
boxCornerRadiusTopLeftId int: the resource to use for the box's top left corner radius

boxCornerRadiusTopRightId int: the resource to use for the box's top right corner radius

boxCornerRadiusBottomRightId int: the resource to use for the box's bottom right corner radius

boxCornerRadiusBottomLeftId int: the resource to use for the box's bottom left corner radius

setBoxPadding

void setBoxPadding (int boxPaddingLeft, 
                int boxCollapsedPaddingTop, 
                int boxExpandedPaddingTop, 
                int boxPaddingRight, 
                int boxCollapsedPaddingBottom, 
                int boxExpandedPaddingBottom)

Set the box's padding.

Parameters
boxPaddingLeft int: the value in pixels to use for the box's left padding

boxCollapsedPaddingTop int: the value in pixels to use for the box's collapsed top padding

boxExpandedPaddingTop int: the value in pixels to use for the box's expanded top padding

boxPaddingRight int: the value in pixels to use for the box's right padding

boxCollapsedPaddingBottom int: the value in pixels to use for the box's collapsed bottom padding

boxExpandedPaddingBottom int: the value in pixels to use for the box's expanded bottom padding

setBoxStrokeColor

void setBoxStrokeColor (int boxStrokeColor)

Set the outline box's stroke color.

Calling this method when not in outline box mode will do nothing.

Parameters
boxStrokeColor int: the color to use for the box's stroke

setCounterEnabled

void setCounterEnabled (boolean enabled)

Whether the character counter functionality is enabled or not in this layout.

Parameters
enabled boolean

setCounterMaxLength

void setCounterMaxLength (int maxLength)

Sets the max length to display at the character counter.

Parameters
maxLength int: maxLength to display. Any value less than or equal to 0 will not be shown.

setDefaultHintTextColor

void setDefaultHintTextColor (ColorStateList textColor)

Sets the text color used by the hint in both the collapsed and expanded states.

Parameters
textColor ColorStateList

setEnabled

void setEnabled (boolean enabled)

Parameters
enabled boolean

setError

void setError (CharSequence errorText)

Sets an error message that will be displayed below our ERROR(/EditText). If the error is null, the error message will be cleared.

If the error functionality has not been enabled via setErrorEnabled(boolean), then it will be automatically enabled if error is not empty.

Parameters
errorText CharSequence: Error message to display, or null to clear

See also:

setErrorEnabled

void setErrorEnabled (boolean enabled)

Whether the error functionality is enabled or not in this layout. Enabling this functionality before setting an error message via setError(CharSequence), will mean that this layout will not change size when an error is displayed.

Parameters
enabled boolean

setErrorTextAppearance

void setErrorTextAppearance (int resId)

Sets the text color and size for the error message from the specified TextAppearance resource.

Parameters
resId int

setErrorTextColor

void setErrorTextColor (ColorStateList textColors)

Sets the text color used by the error message in all states.

Parameters
textColors ColorStateList

setHelperText

void setHelperText (CharSequence helperText)

Sets a helper message that will be displayed below the ERROR(/EditText). If the helper is null, the helper text functionality will be disabled and the helper message will be hidden.

If the helper text functionality has not been enabled via setHelperTextEnabled(boolean), then it will be automatically enabled if helper is not empty.

Parameters
helperText CharSequence: Helper text to display

See also:

setHelperTextColor

void setHelperTextColor (ColorStateList textColors)

Sets the text color used by the helper text in all states.

Parameters
textColors ColorStateList

setHelperTextEnabled

void setHelperTextEnabled (boolean enabled)

Whether the helper text functionality is enabled or not in this layout. Enabling this functionality before setting a helper message via setHelperText(CharSequence) will mean that this layout will not change size when a helper message is displayed.

Parameters
enabled boolean

setHelperTextTextAppearance

void setHelperTextTextAppearance (int resId)

Sets the text color and size for the helper text from the specified TextAppearance resource.

Parameters
resId int

setHint

void setHint (CharSequence hint)

Set the hint to be displayed in the floating label, if enabled.

Parameters
hint CharSequence

setHintAnimationEnabled

void setHintAnimationEnabled (boolean enabled)

Set whether any hint state changes, due to being focused or non-empty text, are animated.

Parameters
enabled boolean

setHintEnabled

void setHintEnabled (boolean enabled)

Sets whether the floating label functionality is enabled or not in this layout.

If enabled, any non-empty hint in the child EditText will be moved into the floating hint, and its existing hint will be cleared. If disabled, then any non-empty floating hint in this layout will be moved into the EditText, and this layout's hint will be cleared.

Parameters
enabled boolean

setHintTextAppearance

void setHintTextAppearance (int resId)

Sets the hint text color, size, style from the specified TextAppearance resource.

Parameters
resId int

setPasswordVisibilityToggleContentDescription

void setPasswordVisibilityToggleContentDescription (CharSequence description)

Set a content description for the navigation button if one is present.

The content description will be read via screen readers or other accessibility systems to explain the action of the password visibility toggle.

Parameters
description CharSequence: Content description to set, or null to clear the content description

setPasswordVisibilityToggleContentDescription

void setPasswordVisibilityToggleContentDescription (int resId)

Set a content description for the navigation button if one is present.

The content description will be read via screen readers or other accessibility systems to explain the action of the password visibility toggle.

Parameters
resId int: Resource ID of a content description string to set, or 0 to clear the description

setPasswordVisibilityToggleDrawable

void setPasswordVisibilityToggleDrawable (int resId)

Set the icon to use for the password visibility toggle button.

If you use an icon you should also set a description for its action using setPasswordVisibilityToggleContentDescription(CharSequence). This is used for accessibility.

Parameters
resId int: resource id of the drawable to set, or 0 to clear the icon

setPasswordVisibilityToggleDrawable

void setPasswordVisibilityToggleDrawable (Drawable icon)

Set the icon to use for the password visibility toggle button.

If you use an icon you should also set a description for its action using setPasswordVisibilityToggleContentDescription(CharSequence). This is used for accessibility.

Parameters
icon Drawable: Drawable to set, may be null to clear the icon

setPasswordVisibilityToggleEnabled

void setPasswordVisibilityToggleEnabled (boolean enabled)

Returns whether the password visibility toggle functionality is enabled or not.

When enabled, a button is placed at the end of the EditText which enables the user to switch between the field's input being visibly disguised or not.

Parameters
enabled boolean: true to enable the functionality

setPasswordVisibilityToggleTintList

void setPasswordVisibilityToggleTintList (ColorStateList tintList)

Applies a tint to the password visibility toggle drawable. Does not modify the current tint mode, which is ERROR(/PorterDuff.Mode#SRC_IN) by default.

Subsequent calls to setPasswordVisibilityToggleDrawable(Drawable) will automatically mutate the drawable and apply the specified tint and tint mode using ERROR(/DrawableCompat#setTintList(Drawable, ColorStateList)).

Parameters
tintList ColorStateList: the tint to apply, may be null to clear tint

setPasswordVisibilityToggleTintMode

void setPasswordVisibilityToggleTintMode (PorterDuff.Mode mode)

Specifies the blending mode used to apply the tint specified by setPasswordVisibilityToggleTintList(ColorStateList) to the password visibility toggle drawable. The default mode is ERROR(/PorterDuff.Mode#SRC_IN).

Parameters
mode PorterDuff.Mode: the blending mode used to apply the tint, may be null to clear tint

setTypeface

void setTypeface (Typeface typeface)

Set the typeface to use for the hint and any label views (such as counter and error views).

Parameters
typeface Typeface: typeface to use, or null to use the default.

Protected methods

dispatchRestoreInstanceState

void dispatchRestoreInstanceState ( container)

Parameters
container

drawableStateChanged

void drawableStateChanged ()

onLayout

void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

Parameters
changed boolean

left int

top int

right int

bottom int

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

Parameters
state Parcelable