Search code examples
androidsearchview

Need correct if statement code for button that closes the searchView


I have a searchView in my app that when you click on the search icon it displays searchView and the <-- button closes searchView. The code for displaying the <-- button is...

 actionBar.setDisplayHomeAsUpEnabled(true);

enter image description here

What if statement would I need to code my <-- button so I can code what it does.

if(actionBar.setDisplayHomeAsUpEnabled(true)) {}  

The above code doesn't work. There is also no...

if(actionBar.onClick){}

I'm looking for an if statement that says if that <-- button is clicked on, close the searchView.

if(onBackPressed){}  //doesn't work either

Solution

  •  @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if(android.R.id.home == item.getItemId()){
                //write your code here
            }
            return super.onOptionsItemSelected(item);
        }