belongs to Maven artifact com.android.support:design:27.1.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).
TheTextInputEditText
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 |
CharSequence
|
getError()
Returns the error message that was set to be displayed with
|
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 |
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
|
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
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 |
getCounterMaxLength
int getCounterMaxLength ()
Returns the max length shown at the character counter.
Related XML Attributes:
Returns | |
---|---|
int |
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 |
See also:
getHint
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
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.
Related XML Attributes:
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.
Related XML Attributes:
Returns | |
---|---|
boolean |
See also:
isErrorEnabled
boolean isErrorEnabled ()
Returns whether the error functionality is enabled or not in this layout.
Related XML Attributes:
Returns | |
---|---|
boolean |
See also:
isHintAnimationEnabled
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
boolean isHintEnabled ()
Returns whether the floating label functionality is enabled or not in this layout.
Related XML Attributes:
Returns | |
---|---|
boolean |
See also:
isPasswordVisibilityToggleEnabled
boolean isPasswordVisibilityToggleEnabled ()
Returns whether the password visibility toggle functionality is currently enabled.
Returns | |
---|---|
boolean |
setCounterEnabled
void setCounterEnabled (boolean enabled)
Whether the character counter functionality is enabled or not in this layout.
Related XML Attributes:
Parameters | |
---|---|
enabled |
boolean |
setCounterMaxLength
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
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
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
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
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
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
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
void setHintTextAppearance (int resId)
Sets the hint text color, size, style from the specified TextAppearance resource.
Related XML Attributes:
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.
Related XML Attributes:
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.
Related XML Attributes:
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.
Related XML Attributes:
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.
Related XML Attributes:
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.
Related XML Attributes:
Parameters | |
---|---|
enabled |
boolean : true to enable the functionality |
setPasswordVisibilityToggleTintList
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
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
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 |
Annotations
Interfaces
- AppBarLayout.OnOffsetChangedListener
- BaseTransientBottomBar.ContentViewCallback
- BottomNavigationView.OnNavigationItemReselectedListener
- BottomNavigationView.OnNavigationItemSelectedListener
- CoordinatorLayout.AttachedBehavior
- NavigationView.OnNavigationItemSelectedListener
- SwipeDismissBehavior.OnDismissListener
- TabLayout.OnTabSelectedListener
Classes
- AppBarLayout
- AppBarLayout.Behavior
- AppBarLayout.Behavior.DragCallback
- AppBarLayout.Behavior.SavedState
- AppBarLayout.LayoutParams
- AppBarLayout.ScrollingViewBehavior
- BaseTransientBottomBar
- BaseTransientBottomBar.BaseCallback
- BottomNavigationView
- BottomSheetBehavior
- BottomSheetBehavior.BottomSheetCallback
- BottomSheetBehavior.SavedState
- BottomSheetDialog
- BottomSheetDialogFragment
- CollapsingToolbarLayout
- CollapsingToolbarLayout.LayoutParams
- CoordinatorLayout
- CoordinatorLayout.Behavior
- CoordinatorLayout.LayoutParams
- CoordinatorLayout.SavedState
- FloatingActionButton
- FloatingActionButton.Behavior
- FloatingActionButton.OnVisibilityChangedListener
- NavigationView
- NavigationView.SavedState
- Snackbar
- Snackbar.Callback
- SwipeDismissBehavior
- TabItem
- TabLayout
- TabLayout.Tab
- TabLayout.TabLayoutOnPageChangeListener
- TabLayout.ViewPagerOnTabSelectedListener
- TextInputEditText
- TextInputLayout
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-04-11 UTC.