1

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!

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Jordi Sipkens
  • 595
  • 1
  • 9
  • 25
  • 1
    why are you requesting focus when you don't want? – karan Apr 15 '19 at 10:48
  • Are u sure that `getView()` in `getView().requestFocus()` returns parent view? Looks like you requesting focus for the same view instead of parent. – Stan Apr 15 '19 at 10:51
  • Yeah quite sure it refers to the Fragment.getView();. I've also tried it with other views. – Jordi Sipkens Apr 15 '19 at 11:13
  • Is the searchView your last focusable view in your XML? Because if it is you are facing an Android system problem. You see, you try to clear focus out of the view, but then it has no other View to focus on, thats why the cursor does not disappear – Ricardo Apr 15 '19 at 12:05
  • https://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus – Quick learner Apr 15 '19 at 12:48
  • Ricardo, no it's not the last focusable item in my XML. I have multiple other views before and after that can be focused. But I've solved the issue by using a seperate view for the button instead of the build in drawable function of EditText. – Jordi Sipkens Apr 15 '19 at 12:57
  • Quick Learner -> Not at all my problem. Might want to read the question a bit better. – Jordi Sipkens Apr 15 '19 at 12:57

2 Answers2

1

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.

0

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.

Jordi Sipkens
  • 595
  • 1
  • 9
  • 25