androidandroid-textinputedittextonfocuschangelistener

Weird behaviour of TextInputEditText if onFocusChangeListener is hooked


I have two EditText in my Activity, each of one is inside TextInputLayout:

<android.support.design.widget.TextInputLayout
            android:id="@+id/tilm"
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:layout_below="@+id/logo"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            android:textColorHint="#FFFFFF"
            app:hintEnabled="false">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/etlogin"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:background="@drawable/round_edittext"
                android:hint="@string/email"
                android:textColorHint="#FFFFFF"
                android:gravity="center"
                android:padding="5dp"
                android:textColor="#FFFFFF"

                android:inputType="textEmailAddress" />
        </android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
            android:id="@+id/til"
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:layout_below="@+id/tilm"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true"
            app:passwordToggleEnabled="true"
            android:textColorHint="#FFFFFF"
            app:hintEnabled="false">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/etpass"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:background="@drawable/round_edittext"
                android:hint="@string/pass"
                android:textColorHint="#FFFFFF"
                android:gravity="center"
                android:padding="5dp"
                android:textColor="#FFFFFF"

                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout>

Also, in code, I have the same onFocusChangeListener hooked:

focusChangeListener=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            if(hasFocus){
                scrollView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                }, 200);
            }
        }
    };

(This is in order that when the keyboard appears, an ScrollView that is in my layout goes down, so the user can see other elements of the UI that are below, buttons specifically).

Also, both TextInputEditText have a TextWatcher:

textWatcher=new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(ValidateLogin(login.getText().toString()) && pass.getText().length()>0){
                entrar.setEnabled(true);//This is a button
            }else{
                if(entrar.isEnabled()){
                    entrar.setEnabled(false);
                }
            }
        }
    };
login.addTextChangedListener(textWatcher);
    pass.addTextChangedListener(textWatcher);
    login.setOnFocusChangeListener(focusChangeListener);
    pass.setOnFocusChangeListener(focusChangeListener);

The problem is, when the second TextInputEditText gain focus, the cursor goes automatically to the first one, and I can't type anything in the second one, since the focus goes immediately to the first one. I'd like this not to happen, keep my onFocusChangeListener in both TextInputEditText, and not change the focus automatically.

I have tried to make another OnFocusChangeListener for the second TextInputEditText, with no luck. If I remove the onFocusChangeListener, the focus maintains in the TextInputEditText, but obviously the ScrollView doesn't moves.

Any help?

Thank you.


Solution

  • The documentation of ScrollView.fullScroll() says:

    This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area.

    The problem probably is that the first TextInputEditText is the "topmost component in the new visible area" of the ScrollView, hence it is given focus automatically.

    Possible solutions could be: