27

I'm using a search view in my application. Now i just want to get the text typed in the SearchView text box and display it on another textview. If i typed the text and click a button i can do the same. But i don't want to use any extra buttons. I just want to display the result when i am pressing enter key.

Janusz
  • 187,060
  • 113
  • 301
  • 369
andro-girl
  • 7,989
  • 22
  • 71
  • 94

7 Answers7

54

Try to use setOnQueryTextListener of SearchView

new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextChange(String newText) {
        // your text view here
        textView.setText(newText);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        textView.setText(query);
        return true;
    }
}
flakes
  • 21,558
  • 8
  • 41
  • 88
HighFlyer
  • 1,615
  • 2
  • 15
  • 22
  • 2
    It works fine. You had to add a listener to your searchview first. – Asim Feb 07 '13 at 05:09
  • 2
    No, listener or not, onQueryTextSubmit does not work when the string entered is empty. – Yoann Hercouet May 30 '13 at 14:04
  • @HighFlyer is there any event on load of searchview (after it's expanded and keyboard is shown). I have been struggling with this since long time. [Question Link](http://stackoverflow.com/questions/25158248/is-there-any-listener-on-android-searchview-to-notify-if-searchview-is-expanded/25158420) – Pradeep Sharma Aug 07 '14 at 11:45
25

The above answer is good but not complete actually you need to set an action listener for your Search view . you can do this in two ways create a class that implements the necessary classes to be an OnQueryTextListener and make a new object of that and use it as your search view query text listener or use the following compact form:

        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                callSearch(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
//              if (searchView.isExpanded() && TextUtils.isEmpty(newText)) {
                    callSearch(newText);
//              }
                return true;
            }

            public void callSearch(String query) {
                //Do searching
            }

        });
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mr.Q
  • 4,316
  • 3
  • 43
  • 40
7

It also can be done with RXAndroid and RxBinding by Jake Wharton like this:

RxSearchView.queryTextChanges(searchView)
            .debounce(500, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<CharSequence>() {
                @Override
                public void call(CharSequence charSequence) {
                    if(charSequence!=null){
                        // Here you can get the text
                        System.out.println("SEARCH===" + charSequence.toString());
                    }
                }
            });

Code is subscribing to observe text change with some delay of 500 milliseconds.

This is a link to git repo to get RXAndroid: https://github.com/JakeWharton/RxBinding

Bogdan Ustyak
  • 5,639
  • 2
  • 21
  • 16
5

In Kotlin, you can do :

searchView.setOnQueryTextListener(object : OnQueryTextListener {

    override fun onQueryTextChange(newText: String): Boolean {
        return false
    }

    override fun onQueryTextSubmit(query: String): Boolean {
        // task HERE
        return false
    }

})
Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
3

The right way to solve this is to use setOnQueryTextListener

This is a small exmple using Kotlin:

txtSearch = rootView.searchView 
txtSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

        override fun onQueryTextChange(newText: String): Boolean {
            return false
        }

        override fun onQueryTextSubmit(query: String): Boolean {
            return false
        }
 })
Ivan Vovk
  • 929
  • 14
  • 28
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – double-beep May 22 '19 at 11:43
1

This is what I have inside the onCreateOptionsMenu(Menu menu) function on my main activity:

MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        // Do whatever you need. This will be fired only when submitting.
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // Do whatever you need when text changes. 
        // This will be fired every time you input any character.
        return false;
    }
});
Acemond
  • 86
  • 1
  • 7
1

with extension functions, create SearchViewExtensions.kt

import android.view.View
import androidx.appcompat.widget.SearchView

inline fun SearchView.onQueryTextChanged(crossinline listener: (String) -> Unit) {
    this.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            return true
        }

        override fun onQueryTextChange(newText: String?): Boolean {
            listener(newText.orEmpty())
            return true
        }
    })
}

and the magic in your fragment

mSearchView.onQueryTextChanged { query: String ->
     // do whatever
}
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111