AndroidDev11
AndroidDev11

Reputation: 103

How to disable floating label animation when populating EditText Fields wrapped in TextInputLayout

I would like to be able to have the floating label already in place when I pre-populate the EditText field. When the view loads, the hint is still displayed behind the text before it is animated to the floating label. There doesn't seem to be a method for this in Support Library's TextInputLayout. Any thoughts?

Upvotes: 9

Views: 6976

Answers (3)

varotariya vajsi
varotariya vajsi

Reputation: 4041

I got solution using below code, i tried using xml but not working longer, you can try as to set hint programatically to both TextInputLayout and TextInputEditText

    TextInputLayout hintView = (TextInputLayout) findViewById(R.id.activity_login_inputlayout_password);
    hintView.setHintAnimationEnabled(false);
    hintView.setHint("");
    yourEditTextPassword.setHint(getString(R.string.text_password_hint));

Upvotes: 0

EE66
EE66

Reputation: 4661

Based Gabriels answer 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: 3

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364644

With the support design library v23 you can use:

til.setHintAnimationEnabled(false);

Here you can find the javadoc.

Upvotes: 9

Related Questions