Android design support library: TextInputLayout – floating label

Android design support library introduced TextInputLayout – floating label and some other important new widget that helps developers to create consistent UI following material design guidelines.
One of the new component shipped by Android design support library is TextInputLayout that is used to animate EditText labels. To do the job, TextInputLayout uses an EditText attribute, called hints.

Android Floating label

To show how to use TextInputLayout – floating label and EditText, we will create an android app like the one shown below:

Using TextInputLayout, and wrapping EditText inside TextInputLayout, it is possible to control this hint. It disappears as soon as the user starts writing the text and “moves” to the label position:

Another important feature is the error handling. With TextInputLayout it is possible to show error messages near to the EditText.

How to use TextInputLayout – floating label and EditText

As said before, TextInputLayout wraps the EditText so that it controls the EditText behaviour. Let us suppose we have three different EditText and three TextInputLayout. Focusing the attention to the first one of this EditText, this one has to be wrapped by TextInputLayout so that we can have the floating label.

<RelativeLayout xmlns:android="" xmlns:tools="" xmlns:app="" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    < android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/til1">
      <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edtName" android:ems="10" android:hint="@string/hintName"/>

   < android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/til2" android:layout_below="@id/til1" android:layout_marginTop="20dp">

    <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edtSurname" android:ems="10" android:hint="@string/hintSurname"/>
  < android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/til3" android:layout_below="@id/til2" android:layout_marginTop="20dp">

    <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edtPhone" android:ems="10" android:hint="@string/hintPhone" android:inputType="phone"/>
  <Button android:layout_width="120dp" android:layout_height="wrap_content" android:id="@+id/btn" android:text="@string/btnConfirm" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:elevation="2dp"/>


android:hint is the floating label that is managed the TextInputLayout. With this layout and a simple activity,  it is possible to obtain the app shown above.

Android Form Validation

Another interesting use of the TextInputLayout – floating label is in the form validation. As told before, this component handles error messages so that they appear near the EditText.
This feature is very useful when the app has to validate some data inserted by an user. To show how to use TextInputLayout to handle EditText error we can suppose to have a simple form with a submit button as shown in the picture below.

When an user pushes the button, the app validates the result and if something goes wrong, the app shows the error message near the EditText that caused it.

To handle the errors, TextInputLayout provides two methods: setError that accepts a string that contains the message and setErrorEnabled that accepts a boolean to enable or disable the error functionality. The first method is used to show an erorr message, the second to remove the message.
The Activity that handles the UI contains validation method, like the one shown below:

edtName = (EditText) findViewById(;
edtSurname = (EditText) findViewById(;
edtPhone = (EditText) findViewById(;
nameLayout = (TextInputLayout) findViewById(;
surnameLayout = (TextInputLayout) findViewById(;
phoneLayout = (TextInputLayout) findViewById(;

Button btn = (Button) findViewById(;
btn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
     if ( validateData() ) {

In the validate method:

 private boolean validateData() {
  boolean result = true;

  String name = edtName.getText().toString();
  if (name == null || name.length() &lt; 3) {
    // We set the error message
    result = false;
    // We remove error messages

  String surname = edtSurname.getText().toString();
  if (surname == null || surname.length() &lt; 3) {
    // We set the error message
    result = false;
    // We remove error messages

  String phone = edtPhone.getText().toString();
  if (phone == null || phone.equals("")) {
    result = false;
  else if (phone.equals("0")) {
    result = false;

  return result;

In this post, you have learnt how to create an Android app interface using material design and Android design support library: you saw how  TextInputLayout animates EditText and handles error using the combination of TextInputLayout and EditText.
Android app source code @github

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.