Search code examples
androidhiddensearchviewinvisiblecardview

Hiding Cardview until search is initiated


I've followed the code on http://www.camposha.info/source/android-recyclerview-search-filter and made my search filter work. The only thing I need is when opening the activity to keep the Cardview hidden, or invisible until the search has started in the searchbar (in other words, to keep the Cardview hidden until I start typing something in the searchbar). I have read the comments which I should have edited the Edittext but I don't have it. what should I do?


Solution

  • Keep the cardview's visibility gone in the layout. And add a queryTextListener to your searchView

    yourSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            if(newText.length()>0){
            yourCardView.setVisibility(View.VISIBLE);
            }else{
            yourCardView.setVisibility(View.GONE);
            }
            return true;
        }
    
        @Override
        public boolean onQueryTextSubmit(String query) {
    
            return true;
        }
    }