TextInputLayout and EditText double hint issue

karsas picture karsas · Jul 6, 2015 · Viewed 19.6k times · Source

I want to set the hint with java in EditText(which is in TextInputLayout).

Code used for setting hint:

aET = (EditText) findViewById(R.id.aET); aET.setHint("h?");

But even when edittext is focused, Hint is displayed twice(inside edittext also).

Please let me know if anyone had faced and found some workaround

when editText is focused...

Second

when editText is not focused..

First

EDIT[10th July 2015]:

<android.support.design.widget.TextInputLayout
     android:id="@+id/aTIL"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
     <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/aET" />
</android.support.design.widget.TextInputLayout>

Answer

neits picture neits · Jul 9, 2015

This problem occurs because the hint from the xml is passed on to the TextInputLayout, so it can be displayed as a floating hint. But when you set it programatically, the hint is set to the EditText, which then results in two hints (one from the xml that is a floating hint and one you set programatically and is a normal EditText hint). If you wish to change the floating hint, you must set the hint on TextInputLayout.

You code will then look like this:

aTIL = (TextInputLayout) findViewById(R.id.aTIL);
aTIL.setHint("h?");