Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Hide and reopen menu icons from Action Bar when search view expand's and close respectively

    • 0
    • 7
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.13k
    Comment on it

    To hide other menu items from ActionBar/Toolbar when SearchView is expanded or open you need to apply OnSearchClickListener on it.
    On the onCreateOptionsMenu method set OnSearchClickListener like this:-

    // hide other menu item when SearchView is open or expanded
    searchView.setOnSearchClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (menu != null) {
             menu.findItem(R.id.action_add_reminder).setVisible(false);
             menu.findItem(R.id.action_logout).setVisible(false);
          }
       }
    });
    

    Now to reopen or show again the other menu items you need to set the OnCloseListener on the SearchView

     // show other menu item when SearchView close
    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
       @Override
        public boolean onClose() {
        // re-show the action button
        if (menu != null) {
            menu.findItem(R.id.action_add_reminder).setVisible(true);
            menu.findItem(R.id.action_logout).setVisible(true);
          }
          return false;
        }
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: