Search code examples
androidevent-handlingsearchview

ActionBar search view, how to handle left arrow click?


Here is the picture: enter image description here

How can I handle left arrow click? I did my search through intent, that is OK:

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        mFilter = intent.getStringExtra(SearchManager.QUERY);
        ...search query
    }
}

and my close button code:

ImageView closeButton = (ImageView) searchView.findViewById(R.id.search_close_btn);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
...
}

How can I handle left arrow click?


Solution

  • // When using the support library, the setOnActionExpandListener() method is
    // static and accepts the MenuItem object as an argument
    MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // Do something when collapsed
        return true;  // Return true to collapse action view
    }
    
    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // Do something when expanded
        return true;  // Return true to expand action view
    }
    });
    

    This is from here:https://stackoverflow.com/a/18186164/4200187