Skip to content

Instantly share code, notes, and snippets.

@ahmmedrejowan
Created February 3, 2023 21:15
Show Gist options
  • Save ahmmedrejowan/1258ea49daa375a0a27f93a162fd0b88 to your computer and use it in GitHub Desktop.
Save ahmmedrejowan/1258ea49daa375a0a27f93a162fd0b88 to your computer and use it in GitHub Desktop.
SearchView setOnCloseListener Alternate way to make it work
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//////////////// Your codes
SearchView searchView = findViewById(R.id.search_view);
int searchCloseButtonId = searchView.findViewById(androidx.appcompat.R.id.search_close_btn).getId();
ImageView closeButton = searchView.findViewById(searchCloseButtonId);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchView.setQuery("", false);
searchView.clearFocus();
binding.searchResultLayout.setVisibility(GONE);
hideKeyboard();
}
});
}
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(binding.searchView.getWindowToken(), 0);
}
@ahmmedrejowan
Copy link
Author

SearchView setOnCloseListener is a complicated thing in Android. The above is a solution to solve the issues of setOnCloseListener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment