6

OK, this question has been asked many times and I have looked through many topics about it but I can not get it to work.

My searchView is not on the actionBar so I can't use collapseActionView()

I have tried

searchView.clearFocus();//this closes the keyboard but does not remove focus
getActivity().getCurrentFocus().clearFocus(); //basically the same as above
searchView.setQuery("", false); //this clears the query and drops the keyboard
searchView.setIconified(true);  // but the search retains focus and keyboard pops up again

No matter what I try I can not remove the focus from the searchView after it has been selected

Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
Gooner
  • 387
  • 2
  • 23
  • 1
    Have you tried setting the focus to any other dummy view ? check this http://stackoverflow.com/questions/6117967/how-to-remove-focus-without-setting-focus-to-another-control – Ramesh Sep 14 '15 at 11:48
  • @Ramesh thank you very much, that thread helped me out. I tried the dummy view first, but did not work. Probably doing something wrong. I did get it to work by setting my layout to be focusable and setting that when I have made my search query – Gooner Sep 14 '15 at 13:19
  • it would be good to post working solution as answer i hope :) – Ramesh Sep 14 '15 at 13:31

1 Answers1

10

To clear focus from my searchView after executing my query I first had to add

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

to my parent layout of the activity. Then in the activity

    LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.my_layout);
    myLayout.requestFocus();

This will take the focus away from the searchView. All the info I needed was here How to remove focus without setting focus to another control?

Community
  • 1
  • 1
Gooner
  • 387
  • 2
  • 23