User input errors

Floating labels

From google material design documentation.

Help users fix input errors as soon as they are detected. Disable the submission of a form if errors are detected, and if detected only after form submission, clearly explain the error and how to fix it.

How to add?

I. In your build.gradle add latest appcompat and design libraries.

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
    compile 'com.android.support:design:X.X.X' // where X.X.X version
}

II. Make your activity extend android.support.v7.app.AppCompatActivity.

public class MainActivity extends AppCompatActivity {  
    ...
}

III. Declare your EditText inside any layout.xml file and wrap it with TextInputLayout.

<android.support.design.widget.TextInputLayout  
    android:id="@+id/inputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/First_name" />

</android.support.design.widget.TextInputLayout>  

IV. To show/hide error use TextInputLayout#setError.

TextInputLayout inputLayout = (TextInputLayout) findViewById(R.id.inputLayout);  
inputLayout.setError("First name is required"); // show error  
inputLayout.setError(null); // hide error  

Declare app:errorEnabled="true" attribute inside TextInputLayout to reserve space for error label below EditText.

How to style?

Floating labels

I. Declare custom styles in your styles.xml file.

<!--Error label text style-->  
<style name="MyErrorText" parent="TextAppearance.AppCompat.Small">  
    <item name="android:textColor">@color/pink</item>
</style>

<!--Input field style-->  
<style name="MyEditText" parent="Theme.AppCompat.Light">  
    <item name="colorControlNormal">@color/indigo</item>
    <item name="colorControlActivated">@color/pink</item>
</style>  

II. Apply this styles to your TextInputLayout via app:errorTextAppearance attribute and to EditText via android:theme attribute.

<android.support.design.widget.TextInputLayout  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorTextAppearance="@style/MyErrorText"
    app:errorEnabled="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/Title"
        android:theme="@style/MyEditText" />

</android.support.design.widget.TextInputLayout>  

Issues

I. TextInputLayout comes with floating label and there is not way to disable it.
Reproduced in: all versions till v23.1.1

AOSP Issue Tracker link

II. TextInputLayout label/hint is colored to red when setError method is triggered.
Reproduced in: only v23.1.1

AOSP Issue Tracker link