Skip to content

Most visited

Recently visited

navigation
added in version 24.1.0
belongs to Maven artifact com.android.support:design:27.0.0

TextInputLayout

public class TextInputLayout
extends LinearLayout

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.support.design.widget.TextInputLayout


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

Also supports showing an error via setErrorEnabled(boolean) and setError(CharSequence), and a character counter via setCounterEnabled(boolean).

Password visibility toggling is also supported via the setPasswordVisibilityToggleEnabled(boolean) API and related attribute. If enabled, 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.

Note: When using the password toggle functionality, the 'end' compound drawable of the EditText will be overridden while the toggle is enabled. To ensure that any existing drawables are restored correctly, you should set those compound drawables relatively (start/end), opposed to absolutely (left/right).

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:
 <android.support.design.widget.TextInputLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">

     <android.support.design.widget.TextInputEditText
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:hint="@string/form_username"/>

 </android.support.design.widget.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 findViewById(int).

Summary

XML attributes

TextInputLayout_android_hint  
TextInputLayout_counterEnabled  
TextInputLayout_counterMaxLength  
TextInputLayout_errorEnabled  
TextInputLayout_errorTextAppearance  
TextInputLayout_hintAnimationEnabled  
TextInputLayout_hintEnabled  
TextInputLayout_hintTextAppearance  
TextInputLayout_passwordToggleContentDescription  
TextInputLayout_passwordToggleDrawable  
TextInputLayout_passwordToggleEnabled  
TextInputLayout_passwordToggleTint  
TextInputLayout_passwordToggleTintMode  

Inherited constants

From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

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 getCounterMaxLength()

Returns the max length shown at the character counter.

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.

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 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 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 setEnabled(boolean enabled)
void setError(CharSequence error)

Sets an error message that will be displayed below our 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 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 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(SparseArray<Parcelable> 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 android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

TextInputLayout_android_hint

Related methods:

TextInputLayout_counterEnabled

Related methods:

TextInputLayout_counterMaxLength

Related methods:

TextInputLayout_errorEnabled

Related methods:

TextInputLayout_errorTextAppearance

Related methods:

TextInputLayout_hintAnimationEnabled

Related methods:

TextInputLayout_hintEnabled

Related methods:

TextInputLayout_hintTextAppearance

Related methods:

TextInputLayout_passwordToggleContentDescription

Related methods:

TextInputLayout_passwordToggleDrawable

Related methods:

TextInputLayout_passwordToggleEnabled

Related methods:

TextInputLayout_passwordToggleTint

Related methods:

TextInputLayout_passwordToggleTintMode

Related methods:

Public constructors

TextInputLayout

added in version 24.1.0
TextInputLayout (Context context)

Parameters
context Context

TextInputLayout

added in version 24.1.0
TextInputLayout (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

TextInputLayout

added in version 24.1.0
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

getCounterMaxLength

added in version 24.1.0
int getCounterMaxLength ()

Returns the max length shown at the character counter.

Related XML Attributes:

Returns
int

getEditText

added in version 24.1.0
EditText getEditText ()

Returns the EditText used for text input.

Returns
EditText

getError

added in version 24.1.0
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

See also:

getHint

added in version 24.1.0
CharSequence getHint ()

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

Related XML Attributes:

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

getPasswordVisibilityToggleContentDescription

added in version 25.1.0
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

added in version 25.1.0
Drawable getPasswordVisibilityToggleDrawable ()

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

Related XML Attributes:

Returns
Drawable

See also:

getTypeface

added in version 24.1.0
Typeface getTypeface ()

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

Returns
Typeface

isCounterEnabled

added in version 24.1.0
boolean isCounterEnabled ()

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

Related XML Attributes:

Returns
boolean

See also:

isErrorEnabled

added in version 24.1.0
boolean isErrorEnabled ()

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

Related XML Attributes:

Returns
boolean

See also:

isHintAnimationEnabled

added in version 24.1.0
boolean isHintAnimationEnabled ()

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

Related XML Attributes:

Returns
boolean

See also:

isHintEnabled

added in version 24.1.0
boolean isHintEnabled ()

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

Related XML Attributes:

Returns
boolean

See also:

isPasswordVisibilityToggleEnabled

added in version 25.1.0
boolean isPasswordVisibilityToggleEnabled ()

Returns whether the password visibility toggle functionality is currently enabled.

Returns
boolean

See also:

onSaveInstanceState

added in version 24.1.0
Parcelable onSaveInstanceState ()

Returns
Parcelable

setCounterEnabled

added in version 24.1.0
void setCounterEnabled (boolean enabled)

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

Related XML Attributes:

Parameters
enabled boolean

setCounterMaxLength

added in version 24.1.0
void setCounterMaxLength (int maxLength)

Sets the max length to display at the character counter.

Related XML Attributes:

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

setEnabled

void setEnabled (boolean enabled)

Parameters
enabled boolean

setError

added in version 24.1.0
void setError (CharSequence error)

Sets an error message that will be displayed below our 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
error CharSequence: Error message to display, or null to clear

See also:

setErrorEnabled

added in version 24.1.0
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.

Related XML Attributes:

Parameters
enabled boolean

setErrorTextAppearance

added in version 25.1.0
void setErrorTextAppearance (int resId)

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

Related XML Attributes:

Parameters
resId int

setHint

added in version 24.1.0
void setHint (CharSequence hint)

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

Related XML Attributes:

Parameters
hint CharSequence

See also:

setHintAnimationEnabled

added in version 24.1.0
void setHintAnimationEnabled (boolean enabled)

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

Related XML Attributes:

Parameters
enabled boolean

See also:

setHintEnabled

added in version 24.1.0
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.

Related XML Attributes:

Parameters
enabled boolean

See also:

setHintTextAppearance

added in version 24.1.0
void setHintTextAppearance (int resId)

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

Related XML Attributes:

Parameters
resId int

setPasswordVisibilityToggleContentDescription

added in version 25.1.0
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.

Related XML Attributes:

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

setPasswordVisibilityToggleContentDescription

added in version 25.1.0
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.

Related XML Attributes:

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

setPasswordVisibilityToggleDrawable

added in version 25.1.0
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.

Related XML Attributes:

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

setPasswordVisibilityToggleDrawable

added in version 25.1.0
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.

Related XML Attributes:

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

setPasswordVisibilityToggleEnabled

added in version 25.1.0
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.

Related XML Attributes:

Parameters
enabled boolean: true to enable the functionality

setPasswordVisibilityToggleTintList

added in version 25.1.0
void setPasswordVisibilityToggleTintList (ColorStateList tintList)

Applies a tint to the the password visibility toggle drawable. Does not modify the current tint mode, which is SRC_IN by default.

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

Related XML Attributes:

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

setPasswordVisibilityToggleTintMode

added in version 25.1.0
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 SRC_IN.

Related XML Attributes:

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

setTypeface

added in version 24.1.0
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 (SparseArray<Parcelable> container)

Parameters
container SparseArray

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

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)