Most visited

Recently visited

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).

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>
 

Summary

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". 

android.support.design:counterMaxLength The max length to display in the character counter

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

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". 

android.support.design:hintAnimationEnabled Whether to animate hint state changes. 
android.support.design:hintEnabled Whether the layout's floating label functionality is enabled

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

android.support.design:hintTextAppearance  
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; 

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

Typeface getTypeface()

Returns the typeface used for both the expanded and floating hint.

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.

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

Call this to force a view to update its drawable 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 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 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 setTypeface(Typeface typeface)

Set the typeface to use for both the expanded and floating hint.

Protected methods

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 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: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: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

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)

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

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

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.

getTypeface

Typeface getTypeface ()

Returns the typeface used for both the expanded and floating hint.

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:

onSaveInstanceState

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. The default implementation returns null.

refreshDrawableState

void refreshDrawableState ()

Call this to force a view to update its drawable state. This will cause drawableStateChanged to be called on this view. Views that are interested in the new state should call getDrawableState.

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.

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

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

setTypeface

void setTypeface (Typeface typeface)

Set the typeface to use for both the expanded and floating hint.

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

Protected methods

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

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

Hooray!