Search code examples
androidsearchview

hide and show search view when clicked


In my project I created button and search view , I want to hide the searchview and show/visible when I clicked the button , how can I do that this is xml code

<android.support.v7.widget.SearchView
   android:layout_width="match_parent"
   android:id="@+id/search"
   android:queryHint="Search .."
   android:layout_height="wrap_content">
</android.support.v7.widget.SearchView>


<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="search"
   android:layout_below="@+id/search"
   android:textSize="16sp"
   android:id="@+id/but"/>

And this is java code

  final SearchView se=findViewById(R.id.search);
    Button bt = findViewById(R.id.but);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

I try to write setvisible methode but i can't find it in android 3


Solution

  • Add boolean parameter named flag in code

    boolean flag = false;
    

    Check for the boolean value in onClick to hide/unhide searchview

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if (flag){
             // means true
             serchview.setVisibility(View.INVISIBLE);
             flag = false; 
           }
           else{
             serchview.setVisibility(View.VISIBLE) 
             flag = true;
           }  
    
         }
    });