tuanna
tuanna

Reputation: 767

Listening to Android's ActionView expanding event

I want to do something when users click on look-up icon of a SearchView. After reading the API, i wrote something like those at the end of this post. But my application crashed at launch, here is the stack trace:

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
            at android.support.v7.internal.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:740)
            at com.kradragon.minadictionary.MainActivity.onCreateOptionsMenu(MainActivity.java:187)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2508)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
            at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
            at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
            at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
            at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
            at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
            at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

I searched and found some posts that recommend to use MenuItemCompat instead. I also implemented it (i put that code in comment), my app launched but the event did not trigger.

It'll help if someone can explain for me why using MenuItemCompat although some similar APIs exist in the SDK. And please correct my code also.

Thank in advance :)

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return false;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return false;
        }
    });

//        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
//            @Override
//            public boolean onMenuItemActionExpand(MenuItem item) {
//                showResultsFragment();
//                return false;
//            }
//
//            @Override
//            public boolean onMenuItemActionCollapse(MenuItem item) {
//                return false;
//            }
//        });
//        MenuItemCompat.setActionView(searchItem, searchView);

    return true;
}

main_menu.xml file:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/search"
        app:showAsAction="collapseActionView|always"
        app:actionViewClass="android.widget.SearchView"/>
</menu>

Upvotes: 3

Views: 1480

Answers (1)

hidro
hidro

Reputation: 12541

MenuItem.setOnActionExpandListener() is added in API 14, which means it will not work on API <14. Usually to make such things work on lower APIs, you use 'support' libraries, which are meant to provide backward compatibility with lower APIs for new things.

The Android Support Library package is a set of code libraries that provide backward-compatible versions of Android framework APIs as well as features that are only available through the library APIs. Each Support Library is backward-compatible to a specific Android API level. This design means that your applications can use the libraries' features and still be compatible with devices running Android 1.6 (API level 4) and up.

In your case, MenuItemCompat is part of 'support-v4' library.

Upvotes: 2

Related Questions