user13928463
user13928463

Reputation:

searchView.setOnCloseListener don't work android

I am a new application developer.I try to after completing the search or leaving the search line, I try to do something but setOnCloseListener not work.This question may have been asked a lot time before now, but I could not find an answer to my question.

my code as :

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        // TODO Add your menu entries here
        MenuInflater menuInflater = getActivity().getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        MenuItem menuItem = menu.findItem(R.id.action_search);
         SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (mExampleAdapter==null){

                }else {

                    name = MainActivity.Hiedicon();
                    mExampleAdapter.getFilter().filter(newText);

                }

                return false;

            }

        });

        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                Toast.makeText(getActivity(), "Test masg", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        super.onCreateOptionsMenu(menu, inflater);


    }

<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" >


    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search"
        android:title="Search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always|collapseActionView" />


</menu>

Upvotes: 0

Views: 645

Answers (1)

Wini
Wini

Reputation: 1996

in order make oncloselisterner-->ShowasAcction:always

<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=".SearchActivity">

<item
    android:id="@+id/search"
    android:title="@string/search"
    android:icon="@drawable/ic_search_toolbar"
    app:showAsAction="always"
    app:actionViewClass="android.support.v7.widget.SearchView"/>
 </menu>

in java:-

searchView.setOnCloseListener(new OnCloseListener() {

    @Override
    public boolean onClose() {

        Log.i("SearchView:", "onClose");
        searchView.onActionViewCollapsed();
        return false;
    }
});

Upvotes: 2

Related Questions