Search code examples
androidandroid-recyclerviewvisibilitysearchview

How to hide show recyclerView from SearchView?


I have a a recyclerView which shows a list of item . What i am trying to acheive is when i type a word the recyclerView should disappear and when there is no text then recyclerView should reappear. The problem is recyclerView hides successfully but it does not reappear when i clear the text from the searchView.

Here is the code snippet of callback function

@Override
public boolean onQueryTextChange(String newText) {

    if(newText.length()==0)
    {
        historyRecyclerView.setVisibility(View.VISIBLE);
    }
    else
    {
        historyRecyclerView.setVisibility(View.INVISIBLE);
    }

    // musicAdapter.filter(newText);
    return true;
}

Solution

  • try this

    if(newText == null || newText.length()){
        historyRecyclerView.setVisibility(View.VISIBLE);
    }else{
        historyRecyclerView.setVisibility(View.INVISIBLE);
    }