Advertisement
chayanforyou

SearchView in Toolbar in a Fragment

Nov 10th, 2023
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. private MenuItem menuSearch;
  2. private SearchView searchView;
  3.  
  4. //..............
  5.  
  6. binding.toolbar.inflateMenu(R.menu.menu);
  7. menuSearch = binding.toolbar.getMenu().findItem(R.id.action_search);
  8. searchView = (SearchView) menuSearch.getActionView();
  9. initSearchView();
  10. customizeSearchView();
  11.  
  12. binding.toolbar.setOnMenuItemClickListener(item -> {
  13.     int itemId = item.getItemId();
  14.     if (itemId == R.id.action_filter) {
  15.         View anchor = view.findViewById(R.id.action_filter);
  16.         filterImage(anchor);
  17.         return true;
  18.     }
  19.     return false;
  20. });
  21.  
  22. //..............
  23.  
  24. private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
  25.     @Override
  26.     public void handleOnBackPressed() {
  27.         if (!searchView.isIconified()) {
  28.             menuSearch.collapseActionView();
  29.         } else {
  30.             setEnabled(false);
  31.             requireActivity().getOnBackPressedDispatcher().onBackPressed();
  32.         }
  33.     }
  34. };
  35.  
  36. //..............
  37.  
  38. private void initSearchView() {
  39.     menuSearch.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
  40.         @Override
  41.         public boolean onMenuItemActionExpand(@NonNull MenuItem item) {
  42.             return true;
  43.         }
  44.  
  45.         @Override
  46.         public boolean onMenuItemActionCollapse(@NonNull MenuItem item) {
  47.             return true;
  48.         }
  49.     });
  50.  
  51.     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  52.         @Override
  53.         public boolean onQueryTextSubmit(String query) {
  54.             return false;
  55.         }
  56.  
  57.         @Override
  58.         public boolean onQueryTextChange(String query) {
  59.             return true;
  60.         }
  61.     });
  62. }
  63.  
  64. @SuppressLint("DiscouragedPrivateApi")
  65. private void customizeSearchView() {
  66.     SearchManager searchManager = (SearchManager) requireActivity().getSystemService(Context.SEARCH_SERVICE);
  67.     if (searchManager != null) {
  68.         searchView.setSearchableInfo(searchManager.getSearchableInfo(requireActivity().getComponentName()));
  69.     }
  70.  
  71.     SearchView.SearchAutoComplete searchAutoComplete =
  72.             searchView.findViewById(androidx.appcompat.R.id.search_src_text);
  73.     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  74.         searchAutoComplete.setTextCursorDrawable(R.drawable.my_cursor);
  75.     } else {
  76.         try {
  77.             Field field = TextView.class.getDeclaredField("mCursorDrawableRes");
  78.             field.setAccessible(true);
  79.             field.set(searchAutoComplete, R.drawable.my_cursor);
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement