Created
February 3, 2023 21:15
-
-
Save ahmmedrejowan/1258ea49daa375a0a27f93a162fd0b88 to your computer and use it in GitHub Desktop.
SearchView setOnCloseListener Alternate way to make it work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SearchView setOnCloseListener is a complicated thing in Android. The above is a solution to solve the issues of setOnCloseListener.