Jordi Sipkens
Jordi Sipkens

Reputation: 595

EditText weird focus behaviour

Alright,

When I have the focus on my EditText and the keyboard etc is showing. I click on my drawable at the end of the edittext (Cancel button). I recognize this event with an onTouchListener and hide the keyboard, clear focus, etc myself.

However, when I 'touch' the cancel button, it hides the keyboard but the focus stays on the edittext. Meaning the cursor is still showing, but not blinking. So when I click on the edittext again to get focus, it shows the option to copy/paste, etc like I'm long pressing the cursor and doesn't show the keyboard.

But when I 'press' the cancel button, it clears the focus, hides the keyboard and most importantly it hides the cursor resulting in completely removing the focus. Then when I click on the edittext again, it gets focus back and DOES show the keyboard.

What is this weird behavior and how do I make it that it always does the 'press' behavior.

my code:

searchView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (event.getRawX() >= searchView.getRight() - searchView.getCompoundDrawables()[2].getBounds().width()) {
                    hideKeyboard(v);
                    isSearching = false;
                    searchView.setText("");
                    searchView.clearFocus();
                    searchView.setFocusable(false);
                    searchView.setFocusableInTouchMode(false);
                    getView().requestFocus(); // parent has both focusables true
                    searchView.setFocusable(true);
                    searchView.setFocusableInTouchMode(true);
                    fetchArticles();

                    return true;
                }
            }
            return false;
        }
    });

Like you're seeing, I'm trying everything I can to always get the same behavior. However no luck so far. Hope you guys can help me!

Upvotes: 1

Views: 188

Answers (2)

Dmitriy Pavlukhin
Dmitriy Pavlukhin

Reputation: 419

Try these in parent of that EditText or in views you want to touch:

android:clickable="true"
android:focusable="true" 
android:focusableInTouchMode="true"

It will allow to completely transfer focus elsewhere from EditText

However, in case when your drawable is inside of your Edittext like here:

android:drawableLeft="@drawable/my_icon"

the solution won't work until you make drawable as separate view in layout.

Upvotes: 1

Jordi Sipkens
Jordi Sipkens

Reputation: 595

Oke so my current 'solution' is a suggested solution by @Dmitriy Pavlukhin.

Instead of using the drawableEnd from EditText itself, I created a separate view which has the same drawable and make sure to draw this on the same position. Now when I click this view, it clears the view as desired 100% of the time. The codebase is still the same from the question, however it is now implemented in a OnClick on the separated view.

Upvotes: 0

Related Questions