Skip to content

Most visited

Recently visited

navigation

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

android.support.design:counterEnabled Whether the layout is laid out as if the character counter will be displayed. 
android.support.design:counterMaxLength The max length to display in the character counter. 
android.support.design:errorEnabled Whether the layout is laid out as if an error will be displayed. 
android.support.design:errorTextAppearance TextAppearance of any error message displayed. 
android.support.design:hintAnimationEnabled Whether to animate hint state changes. 
android.support.design:hintEnabled Whether the layout's floating label functionality is enabled. 
android.support.design:hintTextAppearance  
android.support.design:passwordToggleContentDescription Text to set as the content description for the password input visibility toggle. 
android.support.design:passwordToggleDrawable Drawable to use as the password input visibility toggle icon. 
android.support.design:passwordToggleEnabled Whether the view will display a toggle when the EditText has a password. 
android.support.design:passwordToggleTint Icon to use for the password input visibility toggle

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". 

android.support.design:passwordToggleTintMode Blending mode used to apply the background tint. 
android:hint The hint to display in the floating label. 

Inherited XML attributes

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

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)

Adds a child view with the specified layout parameters.

void dispatchProvideAutofillStructure(ViewStructure structure, int flags)

Dispatch creation of ViewStructure down the hierarchy.

This implementation adds in all child views of the view group, in addition to calling the default View implementation.

void draw(Canvas canvas)

Manually render this view (and all of its children) to the given 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()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.

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)

Set the enabled state of this view.

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)

Called by restoreHierarchyState(android.util.SparseArray) to retrieve the state for this view and its children.

void drawableStateChanged()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

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

Called from layout when this view should assign a size and position to each of its children.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onRestoreInstanceState(Parcelable state)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState().

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

android.support.design:counterEnabled

Whether the layout is laid out as if the character counter will be displayed.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.design:counterMaxLength

The max length to display in the character counter.

May be an integer value, such as "100".

Related methods:

android.support.design:errorEnabled

Whether the layout is laid out as if an error will be displayed.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.design:errorTextAppearance

TextAppearance of any error message displayed.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Related methods:

android.support.design:hintAnimationEnabled

Whether to animate hint state changes.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.design:hintEnabled

Whether the layout's floating label functionality is enabled.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.design:hintTextAppearance

Related methods:

android.support.design:passwordToggleContentDescription

Text to set as the content description for the password input visibility toggle.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

Related methods:

android.support.design:passwordToggleDrawable

Drawable to use as the password input visibility toggle icon.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Related methods:

android.support.design:passwordToggleEnabled

Whether the view will display a toggle when the EditText has a password.

May be a boolean value, such as "true" or "false".

Related methods:

android.support.design:passwordToggleTint

Icon to use for the password input visibility toggle

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

Related methods:

android.support.design:passwordToggleTintMode

Blending mode used to apply the background tint.

Must be one of the following constant values.

ConstantValueDescription
multiplyeMultiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc]
screenf[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
src_atop9The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc]
src_in5The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da]
src_over3The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]

Related methods:

android:hint

The hint to display in the floating label.

May be a string value, using '\\;' to escape characters such as '\\n' or '\\uxxxx' for a unicode character;

Related methods:

Public constructors

TextInputLayout

added in version 22.2.0
TextInputLayout (Context context)

Parameters
context Context

TextInputLayout

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

Parameters
context Context

attrs AttributeSet

TextInputLayout

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

Adds a child view with the specified layout parameters.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child View: the child view to add

index int: the position at which to add the child or -1 to add last

params ViewGroup.LayoutParams: the layout parameters to set on the child

dispatchProvideAutofillStructure

void dispatchProvideAutofillStructure (ViewStructure structure, 
                int flags)

Dispatch creation of ViewStructure down the hierarchy.

The default implementation does the following:

When overridden, it must either call super.dispatchProvideAutofillStructure(structure, flags) or explicitly set the AutofillId in the structure (for example, by calling structure.setAutofillId(getAutofillId())).

When providing your implementation you need to decide how to handle the AUTOFILL_FLAG_INCLUDE_NOT_IMPORTANT_VIEWS flag which instructs you to report all views to the structure regardless if isImportantForAutofill() returns true. We encourage you respect the importance property for a better user experience in your app. If the flag is not set then you should filter out not important views to optimize autofill performance in your app.

This implementation adds in all child views of the view group, in addition to calling the default View implementation.

Parameters
structure ViewStructure: Fill in with structured view data.

flags int: optional flags.

draw

void draw (Canvas canvas)

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
canvas Canvas: The Canvas to which the View is rendered.

getCounterMaxLength

added in version 23.1.0
int getCounterMaxLength ()

Returns the max length shown at the character counter.

Related XML Attributes:

Returns
int

getEditText

added in version 22.2.0
EditText getEditText ()

Returns the EditText used for text input.

Returns
EditText

getError

added in version 23.0.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 23.0.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 24.2.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 24.2.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 23.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 23.2.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 23.0.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 23.0.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 23.2.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 24.2.0
boolean isPasswordVisibilityToggleEnabled ()

Returns whether the password visibility toggle functionality is currently enabled.

Returns
boolean

See also:

onSaveInstanceState

added in version 23.2.0
Parcelable onSaveInstanceState ()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.

Returns
Parcelable Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save.

setCounterEnabled

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

Set the enabled state of this view. The interpretation of the enabled state varies by subclass.

Parameters
enabled boolean: True if this view is enabled, false otherwise.

setError

added in version 22.2.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 22.2.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 22.2.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 23.0.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 23.2.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 23.0.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 24.2.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 24.2.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 24.2.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 24.2.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 24.2.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 24.2.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 24.2.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 23.0.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)

Called by restoreHierarchyState(android.util.SparseArray) to retrieve the state for this view and its children. May be overridden to modify how restoring happens to a view's children; for example, some views may want to not store state for their children.

Parameters
container SparseArray: The SparseArray which holds previously saved state.

drawableStateChanged

void drawableStateChanged ()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

If the View has a StateListAnimator, it will also be called to run necessary state change animations.

Be sure to call through to the superclass when overriding this function.

onLayout

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

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view

left int: Left position, relative to parent

top int: Top position, relative to parent

right int: Right position, relative to parent

bottom int: Bottom position, relative to parent

onMeasure

void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

onRestoreInstanceState

void onRestoreInstanceState (Parcelable state)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

Parameters
state Parcelable: The frozen state that had previously been returned by onSaveInstanceState().

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!

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 one-minute survey?
Help us improve Android tools and documentation.