Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 728
AndroidDev11 Avatar asked Aug 20 '15 16:08

AndroidDev11


People also ask

How do I turn off floating hint in TextInputLayout?

Floating Hints are enabled by default in a TextInputLayout. To disable it we need to add the following attribute inside the tag : app:hintEnabled="false" . The below xml code is from the activity_main. xml layout and has three EditText fields.

How do I remove TextInputLayout padding?

By default, some space is reserved for assistive labels below SfTextInputLayout control. These reserved spaces can be removed by setting ReserveSpaceForAssistiveLabels property as false when helper and error text are empty and ShowCharCount is false.

How do I get rid of underline in TextInputLayout Android?

TextInputLayout. OutlinedBox" , the box looks almost the same, but the underline is gone. In order to remove the stroke just set the app:boxStrokeColor attribute to @null . Save this answer.


2 Answers

With the support design library v23 you can use:

til.setHintAnimationEnabled(false);

Here you can find the javadoc.

like image 116
Gabriele Mariotti Avatar answered Sep 29 '22 10:09

Gabriele Mariotti


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);
            }
        }
    }
like image 30
EE66 Avatar answered Sep 29 '22 11:09

EE66