Search code examples
androidtabssearchview

Hide search view when tab is changed


I have two tabs, one tab contains news headlines and other categories. User can search only news headlines. So I want to hide search view when user opens categories tab. I tried this but not working !

@Override
        public void onTabSelected(TabLayout.Tab tab) {
            if(tab.getPosition() == 1)
                searchView.setVisibility(View.GONE);
            viewPager.setCurrentItem(tab.getPosition());
        }

Below is my code.

private void initialize(){
    setContentView(R.layout.activity_sec_main);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    NavigationDrawer navigationDrawer_fragment =
            (NavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.mainfragmentdrawer);
    navigationDrawer_fragment.setup((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("Latest"));
    tabLayout.addTab(tabLayout.newTab().setText("Categories"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    viewPager = (ViewPager) findViewById(R.id.pager);
    adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

initializing search view

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.refresh_activity, menu);
    MenuItem itm = menu.findItem(R.id.action_search);
    searchView = (SearchView) itm.getActionView();
}

Solution

  • You must add an else statement to your onTabSelected because if you set the visibility once, it will not go back to its original form since it is a tab layout. But first you need to set a Boolean to tell the menuItem if it should be shown or not.

    private boolean searchViewShown = false;
    
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
      if(tab.getPosition() == 1) {
         searchViewShown = false;
      } else {
         searchViewShown = true;
      }
    
      invalidateOptionsMenu();
      viewPager.setCurrentItem(tab.getPosition());
    }
    

    Update

    It seems that your SearchView is a menuItem. In that case you need to add onPrepareOptionsMenu, and set here the visibility of your searchView like:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem itm = menu.findItem(R.id.action_search);
        if (searchViewShown) {
            itm.setVisible(true);
        } else { 
            itm.setVisible(false);
        }
        return true
    }
    

    Then add invalidateOptionsMenu() whenever you select a tab, so that it calls the onPrepareOptionsMenu and check if your searchView will be shown or not.