TextInputLayout animation overlaps the text when the text is set programmatically
Currently the only way to avoid this behavior is to add the EditText
programmatically.
- Create a
TextInputLayout
without theEditText
. Programmatically or via XML inflation - doesn't matter, but it has to be empty. - Create the
EditText
and set its text to whatever you need. - Add the
EditTExt
to theTextInputLayout
.
Here's an example:
TextInputLayout til = (TextInputLayout) findViewById(R.id.til);
til.setHint(R.string.hint);
EditText et = new EditText(getContext());
et.setText(value);
til.addView(et);
UPDATED 21/08/2015 WITH DESIGN LIBRARY V23:
With the design support library v23 you can disable the animation:
Just use the setHintAnimationEnabled
method:
textInputLayout.setHintAnimationEnabled(boolean)
Here the issue on Google Tracker.
I ran into this issue recently while using DialogFragment. To solve, simply disable the hint animation if you have a value in the field before you set the field value. The order is important.
For example,
TextInputLayout layout = (TextInputLayout) findViewById(R.id.text_layout);
TextInputEditText edit = (TextInputEditText) findViewById(R.id.text_edit);
String fieldValue = "Something";
layout.setHintAnimationEnabled(fieldValue == null);
edit.setText(fieldValue);
This way the layout does not initiate the animation when the text is set. You can also watch for text changes and enable it again when the field is empty.
You can directly specify in the XML for TextInputLayout
:
app:hintAnimationEnabled="false"