how to listen to keyboard search button in searchView
Asked Answered
A

4

20

I have a SearchView. When the user clicks on the keyboard search button, I need to make a server call. What does the code for the listener look like? I am thinking I have to use OnClickListener. But the internal code for knowing it's the search button, I am not sure how to determine that.

Antipode answered 10/10, 2014 at 19:27 Comment(6)
It sends the KeyEvent#KEYCODE_SEARCH key so you would look for that with an onKeyEventListener or activity's dispatchKeyEventExempt
@Exempt The docs say that onKeyEventListener is for hardware key. Why would it specifically say hardware if it equally applies to the soft keyboard? In any case, I will try it since you say it's the answer.Antipode
@Exempt I already tried onEditorActionListener before asking the question. But the SearchView does not recognize it.Antipode
show some code of your search viewAlbertinaalbertine
@QadirHussain do you even understand the question? Your catch-all comment does not belong here.Visitor
There's an interesting workaround here: SearchView listen for IME actionsHypozeuxis
A
73

I have done like this

the onQueryTextSubmit is the method you are looking for.

set setOnQueryTextListener on your search view.

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

    MenuItem searchItem = menu.findItem(R.id.search_city);
    searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Search View Hint");

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            //Log.e("onQueryTextChange", "called");
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {


            // Do your task here

            return false;
        }

    });

    return true;
}

hope this help

Albertinaalbertine answered 10/10, 2014 at 19:43 Comment(1)
Yay! Ok you beat me to the response. I will up vote!Visitor
Y
2

This is the kotlin version:

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextChange(s: String): Boolean {
                // make a server call
                return true
            }

            override fun onQueryTextSubmit(s: String): Boolean {
                val intent = Intent(applicationContext, YourSearchableActivity::class.java)
                intent.putExtra(SearchManager.QUERY, s)
                intent.putExtra(CUSTOM_MESSAGE, "you can also add custom message when submitting the search action")
                intent.setAction(Intent.ACTION_SEARCH)
                startActivity(intent)
                return true
            }
        })
Yonkers answered 9/8, 2018 at 9:32 Comment(0)
A
2

Here you can go for the Android best answer not Kotlin

        binding.edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // Your piece of code on keyboard search click
                if (AppValidator.isValid(SearchEventsActivity.this, binding.edtSearch, "Please enter your search keyword.")) {
                    searchKeyword = binding.edtSearch.getText().toString().trim();
                    //Search Events categories
                    callSearchEventsApi();
                }

                return true;
            }
            return false;
        }
    });

Make sure you have added following line in your EditText

android:imeOptions="actionSearch"

Just like following:-

 <EditText
                android:id="@+id/edtSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="@dimen/_15dp"
                android:background="@null"
                android:fontFamily="@font/roboto_regular"
                android:textColorHint="@color/text_color_light"
                android:layout_gravity="center"
                android:textSize="@dimen/_16sp"
                android:maxLines="1"
                android:singleLine="true"
                android:imeOptions="actionSearch"
                android:hint="@string/search"/>

I hope this will help you to solve your issue.!!!!

Audreyaudri answered 25/1, 2019 at 7:44 Comment(0)
T
0

SearchView method

setOnQueryTextLister

will do your work as below shown.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                showSnackBar(query.toString());
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

Here searchView is object of SearchView.

Tricornered answered 20/3, 2018 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.