Is there any listener on Android SearchView to notify if SearchView is expanded and ready to take input?

梦想与她 提交于 2019-12-03 12:17:17

You can set an expand/collapse listener on the MenuItem like this:

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

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

This listener was introduced with API level 14, so for backwards compatibility you have to use the v4 support library. If you do then you have to set the expand/collapse listener like this:

MenuItemCompat.setOnActionExpandListener(this.searchItem, new MenuItemCompat.OnActionExpandListener() {

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
    }

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

the questioner wants to know how to know when the searchview is expanded. just do this with your searchview (assuming its standalone but you can get a reference to it from menu item if needed):

searchView.setOnSearchClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //search is expanded
                    }
                });

here is for closed situation:

searchView.setOnCloseListener(new SearchView.OnCloseListener() {
                    @Override
                    public boolean onClose() {
// searchview closed
                        return false;
                    }
                });
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        vwSearch = (SearchView) menu.findItem(R.id.menu_main_search).getActionView();
        vwSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                 //Do something on Submit
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                 //Do something on change
                return true;
            }


        });

        vwSearch.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                //Do something on collapse Searchview
                return false;
            }
        });

        return true;
    }

Try adding this in AndroidManifest.xml along with your activity for example name of your activity is activity1.

<activity
        android:name="com.example.app1.activity1"
        android:label="@string/title_activity_one"
        android:windowSoftInputMode="adjustPan" >
</activity>

instead of "adjustPan" you might use "adjustResize"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!