Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout disable animation

Tags:

android

I am using the TextInputLayout to implement the floating label pattern. However, when I set the text programmatically on the EditText, I still get the animation of the label moving from the control to the label - the same as if the user had clicked it.

I don't want this animation though if I set it programmatically, is this possible? Here is my code:

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

And in the onResume I do:

    TextInputLayout root = (TextInputLayout)findViewById(R.id.root);     EditText et = (EditText)root.findViewById(R.id.editText1);     et.setText("Actual text");     root.setHint("Hint"); 
like image 956
steprobe Avatar asked Jun 08 '15 14:06

steprobe


2 Answers

As of v23 of the support libs, a setHintAnimationEnabled method has been added. Here are the docs. So you can set this new attribute to false in the XML and then programmatically set it to true after you've finished populating the editText. OR simply handle it all programmatically, as needed.

So in your example, that would become something like:

TextInputLayout root = (TextInputLayout)findViewById(R.id.root); root.setHintAnimationEnabled(false); root.setHint("Hint");  EditText et = (EditText)root.findViewById(R.id.editText1); et.setText("Actual text");  // later... root.setHintAnimationEnabled(true); 

Of course, be sure to open your Android SDK Manager and update your Android Support Library to rev. 23 and Android Support Repository to rev. 17 first and then add that to build.gradle via:

compile 'com.android.support:design:23.0.0' 

Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency.

like image 117
gMale Avatar answered Oct 04 '22 00:10

gMale


I have found an (unwieldy) way to do that. Looking into the source code of the TextInputLayout, I've discovered that the only occasion when the class doesn't update it's hint with animation is when the EditText is added to it. The only obstacle is that you can only add it once to the layout, once it is there, it will permanently be tied to it, and there's no way to remove it.

So here's the solution:

  1. Create a TextInputLayout without the EditText. Programmatically or via XML inflation - doesn't matter, but it has to be empty.
  2. Create the EditText and set its text to whatever you need.
  3. Add the EditText to the TextInputLayout.

Here's an example:

TextInputLayout hintView = (TextInputLayout) findViewById(R.id.hint_view); hintView.setHint(R.string.hint);  EditText fieldView = new EditText(hintView.getContext()); fieldView.setText(value);  hintView.addView(fieldView); 

Unfortunately, if you want to set the text to something else without the animation, you will have to repeat all these steps, except for the creation of a new EditText (the last one can be reused). I hope Google can fix this, because it is really inconvenient, but for now that's what we have.

Update: this is, thankfully, fixed in the design library 23.0.0, so just update to that version, and you won't have to do all this crazy stuff.

like image 44
Malcolm Avatar answered Oct 04 '22 00:10

Malcolm