Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MenuItemCompat.setOnActionExpandListener doesn't do anything

Background

I have a searchView being initialized using a special class I've made, that's being used across all of the activities and fragments.

The problem

Recently, probably due to updates to the support library (or because I didn't use it so far, I don't remember), I can't catch events of expand/collapse of the searchView.

As I've found, this happens even if I use setSupportActionBar with a Toolbar instance.

What I've tried

I've tried using each of the next methods, but none worked:

  • MenuItemCompat.setOnActionExpandListener.
  • MenuItemCompat.setOnActionExpandListener together with iconifying the SearchView, as suggested on some websites.
  • setOnActionExpandListener on the search menu item itself, but then it crashes since it can't be used when extending the ActionBarActivity.
  • SearchView.setOnCloseListener , but this works only if I close it, and only using the UI (doesn't get called when calling collapseActionView ).
  • I've also tried to mess around with the XML file of the search menu item.

The code

Here's the helper class I've made:

SearchHolderCompat

public class SearchHolderCompat {
    public MenuItem mSearchMenuItem;
    public SearchView mSearchView;
    private final Activity _context;

    public SearchHolderCompat(final Activity context) {
        _context = context;
    }

    public boolean isCurrentyExpanded() {
        return mSearchMenuItem != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem);
    }

    public boolean hasQuery() {
        return mSearchMenuItem != null && mSearchView != null && MenuItemCompat.isActionViewExpanded(mSearchMenuItem)
                && !TextUtils.isEmpty(mSearchView.getQuery());
    }

    public void addSearchItemAndInit(final Menu menu, final OnQueryTextListener onQueryTextListener,
            final OnActionExpandListener onActionExpandListener) {
        final MenuInflater menuInflater = _context.getMenuInflater();
        menuInflater.inflate(R.menu.search_menu_item, menu);
        init(menu.findItem(R.id.menuItem_search), onQueryTextListener, onActionExpandListener);
    }

    public void init(final MenuItem searchMenuItem, final OnQueryTextListener onQueryTextListener,
            final OnActionExpandListener onActionExpandListener) {
        this.mSearchMenuItem = searchMenuItem;
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
        if (mSearchView == null) {
            MenuItemCompat.setShowAsAction(searchMenuItem, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                    | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
            MenuItemCompat.setActionView(searchMenuItem, mSearchView = new SearchView(_context));
        }
        mSearchView.setQueryHint(_context.getString(R.string.search));
        mSearchView.setOnQueryTextListener(onQueryTextListener);
        MenuItemCompat.setOnActionExpandListener(searchMenuItem, onActionExpandListener);
    }

}

MainActivity.java

public class MainActivity extends ActionBarActivity {
    SearchHolderCompat mSearchHolder = new SearchHolderCompat(this);

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        mSearchHolder.addSearchItemAndInit(menu, new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(final String arg0) {
                android.util.Log.d("AppLog", "onQueryTextSubmit");
                return false;
            }

            @Override
            public boolean onQueryTextChange(final String queryText) {
                android.util.Log.d("AppLog", "onQueryTextChange");
                return true;
            }
        }, new OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(final MenuItem arg0) {
                android.util.Log.d("AppLog", "onMenuItemActionExpand");
                return false;
            }

            @Override
            public boolean onMenuItemActionCollapse(final MenuItem arg0) {
                android.util.Log.d("AppLog", "onMenuItemActionCollapse");
                return false;
            }
        });
        return true;
    }
}

search_menu_item.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <!-- search items -->
    <item
        android:id="@+id/menuItem_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"
        tools:ignore="AlwaysShowAction"/>

</menu>

The question

What's the correct way to handle the SearchView and the search menu item (using the support library) ?

How come "MenuItemCompat.setOnActionExpandListener" doesn't work?

like image 524
android developer Avatar asked Dec 14 '14 12:12

android developer


1 Answers

After looking for a solution for couple of hours I've implement something like this. and worked for me. (I wanted Expand and Collapse events anyhow)
.

@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);



        if(searchItem != null)
        {

            SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
            // use this method for search process
            searchView.setOnSearchClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    //Search view is expanded
                    showSearchPage();
                }
            });
            searchView.setOnCloseListener(new SearchView.OnCloseListener()
            {
                @Override
                public boolean onClose()
                {
                    //Search View is collapsed
                    hideSearchPage();
                    return false;
                }
            });
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
            {

                @Override
                public boolean onQueryTextSubmit(String query)
                {
                    // use this method when query submitted
                    Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText)
                {
                    // use this method for auto complete search process
                    Log.e("SearchValueIs",":"+newText);
                    return false;
                }
            });
        }
        return super.onCreateOptionsMenu(menu);
    }


Hope It will help Someone...

like image 169
GreenROBO Avatar answered Oct 14 '22 13:10

GreenROBO