3

Can't say it much more clearly than in the title. I have an EditText wrapped by a Text Input Layout. I'm trying to trigger an event when that EditText loses focus. However, once the event listener is applied, the TextInputLayout no longer animates the text, it just sits on the editText line until information is entered, at which time it does animate. Can anyone give me some insight as to why this is happening?

Edit: Layout file.

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/card_drawable"
        android:gravity="center"
        android:padding="11dp"
        android:text="The fields below will be used to calculate your training maxes.  Please enter your best rep record, or 1RM, for each lift.  Training maxes will be determined using the following formula provided in Beyond 5/3/1: \n(Weight Lifted x Reps X .0333 + Weight Lifted)" />

    <LinearLayout
        android:id="@+id/squat_container"
        style="@style/linear_layout_style">

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/squatWeight_editText"
                style="@style/editText_floating_label"
                android:hint="Squat Weight" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/txtInputLayoutSquat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/squatReps_editText"
                style="@style/editText_floating_label"
                android:hint="Repetitons" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bench_container"
        style="@style/linear_layout_style">

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/bechpressWeight_editText"
                style="@style/editText_floating_label"
                android:hint="Bench Press Weight" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/benchpressReps_editText"
                style="@style/editText_floating_label"
                android:hint="Repetitons" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/deadlift_container"
        style="@style/linear_layout_style">

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/deadliftWeight_editText"
                style="@style/editText_floating_label"
                android:hint="Deadlift Weight" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/deadliftReps_editText"
                style="@style/editText_floating_label"
                android:hint="Repetitons" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ohp_container"
        style="@style/linear_layout_style">

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/ohpWeight_editText"
                style="@style/editText_floating_label"
                android:hint="Overhead Press Weight" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <EditText
                android:id="@+id/ohpReps_editText"
                style="@style/editText_floating_label"
                android:hint="Repetitons" />
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>


</LinearLayout>
3
  • When focus is away from EditText wrapped in TextInputLayout, it will animate. So, whether is there only one focus on EditText in the full screen or not? If not, try to add another focusable View and then see. Jul 3, 2015 at 2:23
  • The stuff I have inside the listener is working when focus is lost, but when you click into the text box, the hint doesn't "float"
    – Qumbaala
    Jul 3, 2015 at 2:42
  • Could you post your layout file? Jul 3, 2015 at 2:44

2 Answers 2

7

I had to deal with the same issue.

The workaround I used was using a custom EditText and adding a custom listener to it to be called when the onFocusChanged method is fired.

Custom EditText (Boiler plate code is not shown)

public class EditTextPlus extends AppCompatEditText {
    //This is the custom listener you will use
    public OnFocusChangeListener customListener;

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (customListener != null) {
            customListener.onFocusChange(this, focused);
        }
    }
}

In Activity/Fragment

yourEditText.customListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        //Do your stuff
    }
}

Don't forget to use the custom EditText in the xml as well and not the default EditText.

2
  • Hey I'm sorry I haven't responded, but this looks like a good solution. In my case, it seems that a third party library was the culprit. Thank you for your time!
    – Qumbaala
    Oct 16, 2015 at 2:17
  • @Qumbaala I'm glad you worked things out. Anyway I hope this post could help others having this issue.
    – GuyZ
    Oct 16, 2015 at 4:37
7

This doesn't solve the issue but does give some insight (I'll update if I figure out a solution), but seems like the animated hint is dependent on OnFocusChangedListener, which gets overwritten when you add your own listener.

https://code.google.com/p/android/issues/detail?id=175344

EDIT:

Per https://code.google.com/p/android/issues/detail?id=178693...

We're now using a different way to determine if the EditText is focused. For now do something like:

TextInputLayout inputLayout = ...;
EditText editText = inputLayout.getEditText();
final OnFocusChangeListener existing = editText.getOnFocusChangeListener();

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View view, boolean focused) {
        existing.onFocusChange(view, focused);
        // Your custom logic
    }
});
1
  • Thank you for this! I looked at the binaries and suspected it was something like that. My temporary solution was to programmatically add something to the edittext once it gained focus, but this is better.
    – Qumbaala
    Jul 20, 2015 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.