Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private MenuItem menuSearch;
- private SearchView searchView;
- //..............
- binding.toolbar.inflateMenu(R.menu.menu);
- menuSearch = binding.toolbar.getMenu().findItem(R.id.action_search);
- searchView = (SearchView) menuSearch.getActionView();
- initSearchView();
- customizeSearchView();
- binding.toolbar.setOnMenuItemClickListener(item -> {
- int itemId = item.getItemId();
- if (itemId == R.id.action_filter) {
- View anchor = view.findViewById(R.id.action_filter);
- filterImage(anchor);
- return true;
- }
- return false;
- });
- //..............
- private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
- @Override
- public void handleOnBackPressed() {
- if (!searchView.isIconified()) {
- menuSearch.collapseActionView();
- } else {
- setEnabled(false);
- requireActivity().getOnBackPressedDispatcher().onBackPressed();
- }
- }
- };
- //..............
- private void initSearchView() {
- menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
- @Override
- public boolean onMenuItemActionExpand(@NonNull MenuItem item) {
- return true;
- }
- @Override
- public boolean onMenuItemActionCollapse(@NonNull MenuItem item) {
- return true;
- }
- });
- searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
- @Override
- public boolean onQueryTextSubmit(String query) {
- return false;
- }
- @Override
- public boolean onQueryTextChange(String query) {
- return true;
- }
- });
- }
- @SuppressLint("DiscouragedPrivateApi")
- private void customizeSearchView() {
- SearchManager searchManager = (SearchManager) requireActivity().getSystemService(Context.SEARCH_SERVICE);
- if (searchManager != null) {
- searchView.setSearchableInfo(searchManager.getSearchableInfo(requireActivity().getComponentName()));
- }
- SearchView.SearchAutoComplete searchAutoComplete =
- searchView.findViewById(androidx.appcompat.R.id.search_src_text);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- searchAutoComplete.setTextCursorDrawable(R.drawable.my_cursor);
- } else {
- try {
- Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
- field.setAccessible(true);
- field.set(searchAutoComplete, R.drawable.my_cursor);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement