0

I have a SearchView and a button.

The button has an onClick listener that successfully focuses the SearchView, but the keyboard only shows when I actually tap on the SearchView.

I want the keyboard to show in addition to focusing the SearchView when I click the button.

Here's the button listener code inside onCreate:

binding.emptyStateSearch.setOnClickListener {
  searchView.requestFocus() // this works and makes the cursor active in the searchView
  val imm: InputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  imm.showSoftInput(searchView, InputMethodManager.SHOW_IMPLICIT) // but this does not show the keyboard
}

I also tried imm.toggleSoftInput(0, 0) which works but it is deprecated, so I don't want to rely on it.

I tried it without SHOW_IMPLICIT: showSoftInput(searchView, 0). Doesn't work.

I tried putting a timer before calling showSoftInput but that doesn't work either.

The strange thing is that the SearchView.isFocusable returns true but upon calling requestFocus on it, isFocused returns false even though I can clearly see the cursor inside it.

NDeLeon
  • 1
  • 3

1 Answers1

0

Managed to fix it with a simple workaround from this thread: Showing the soft keyboard for SearchView on ActionBar

searchView.setIconified(true);
searchView.setIconified(false);
NDeLeon
  • 1
  • 3