Reputation: 1699
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");
Upvotes: 31
Views: 24565
Reputation: 1012
Put the hint value the to the TextInputEditText instead of TextInputLayout Then add app:hintEnabled="false" to the TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tlFullName"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="25dp"
app:hintEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/fullName"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
Upvotes: 2
Reputation: 2131
Put this attribute app:hintEnabled="false" here:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintEnabled="false">
It works for me
Upvotes: 7
Reputation: 1288
You can disable it by using
app:hintAnimationEnabled="false"
Upvotes: 6
Reputation: 4661
I wrote a small method to run after loading the view hierarchy that disables animation on initial display but enables it after wards. Add this to your Base Activity/Fragment/View and it will solve it issue.
private void setTextInputLayoutAnimation(View view) {
if (view instanceof TextInputLayout) {
TextInputLayout til = (TextInputLayout) view;
til.setHintAnimationEnabled(false);
til.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override public boolean onPreDraw() {
til.getViewTreeObserver().removeOnPreDrawListener(this);
til.setHintAnimationEnabled(true);
return false;
}
});
return;
}
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
setTextInputLayoutAnimation(child);
}
}
}
Upvotes: 1
Reputation: 763
I will give you simple code and you don't have to make all programmatically just add xlm attribute and that's all.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintAnimationEnabled="true"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/password_field_activity_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:hint="@string/password"
android:inputType="textPassword"
android:padding="10dp"
android:textColor="@color/color_holo_light_gray"
android:textColorHint="@color/color_holo_light_gray"
android:textCursorDrawable="@drawable/cursor_drawable" />
</android.support.design.widget.TextInputLayout>
Hope it will help
Upvotes: 3
Reputation: 5371
I check out ideas about support library v23 - it's does not work yet :( Programmatically inserting of EditText is unusable if you wish to use databinding labrary
In result of research I found solution without full disabling of animation:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/product_title_placeholder">
<android.support.design.widget.TextInputEditText
android:id="@+id/text_tiet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={bindingText}"/>
</android.support.design.widget.TextInputLayout>
final TextInputLayout textLayout = (TextInputLayout) findViewById(R.id.text_til);
if(binding.getText() != null) {
// disable animation
textLayout.setHintAnimationEnabled(false);
final TextInputEditText editText = (TextInputEditText) titleLayout.findViewById(R.id.text_tiet);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
// enable animation after layout inflated
textLayout.setHintAnimationEnabled(true);
editText.setOnFocusChangeListener(null);
}
});
}
// manage focus for new binding object
((View)textLayout .getParent()).setFocusableInTouchMode(binding.getText() == null);
It's looks not prettily, but it works :)
Upvotes: 0
Reputation: 41510
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:
TextInputLayout
without the EditText
. Programmatically or via XML inflation - doesn't matter, but it has to be empty.EditText
and set its text to whatever you need.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.
Upvotes: 5
Reputation: 17895
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.
Upvotes: 46