Home > Software design >  Listener for clicking on any area of ​the SearchView
Listener for clicking on any area of ​the SearchView

Time:11-22

I have a SearchView and I am using navigation components. I made such logic that when I click on the search button on the keyboard from fragment A, there is a transition with the transfer of the SearchView text to fragment B.

And here I am in fragment B, and the text from SearchView in fragment A was passed to the input field of SearchView in fragment B, and now I need that when I click on any area of ​​the SearchView, it will go back to fragment A, while android: iconifiedByDefault = "false" , how to do it?

CodePudding user response:

I think this simple code should work.

in Fragment B:

val searchView = view?.findViewById<SearchView>(R.id.yourSearchViewId)
searchView?.setOnClickListener { /* go back to fragment A */ }

EDITED: Made it work with these 2 listeners:

/* this is invoked when you click on anything but the input field */
binding.searchView.setOnTouchListener { v, event -> // ignore all touch events
    /* go back to Fragment A */
    true
}
            
/* this is invoked when you click on the input field */
binding.searchView.setOnQueryTextFocusChangeListener { view, b ->
    /* go back to Fragment A */
}
  • Related