Show Helper text below EditText along with the Hint
The best way is to use TextInputLayout
. Google introduced it in new design library. In order to use the TextInputLayout you have to add the following to your build.gradle dependencies:
compile 'com.android.support:design:22.2.0'
Then use it in your xml files:
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
</android.support.design.widget.TextInputLayout>
You can set your text by setting error true. Altough it is for showing errors but it is suitable for your use. You can change color and typeface if you want.
TextInputLayout til = (TextInputLayout) findViewById(R.id.textInputLayout);
til.setErrorEnabled(true);
til.setError("You need to enter a name");
With Design Support Library 28 , an inbuilt helper Text feature is added in TextInputLayout.
implementation 'com.android.support:design:28.0.0'
Now enable error using xml or programmatically
textInputLayout.isHelperTextEnabled=true
textInputLayout.error="Email can not be Empty!!!"
Also , hint and error can together be used now!!!
Example
et.setOnFocusChangeListener { v, b ->
if (b) {
textInputLayout.helperText = "yourhelperText"
} else {
textInputLayout.helperText = null
if(et.text.toString()==""){ // or any other validation
textInputLayout.error="Email can not be Empty!!!"
}
}
TextInputLayout | Android Developers
EDIT Don't forget to enable error and helperText via xml or programatically.