Get searchView text when search button is clicked [duplicate] - android

This question already has answers here:
How to get a text from SearchView?
(2 answers)
Closed 3 years ago.
I'm new to android programming.
I have a problem with searchview.
when I use textlistener, it ALWAYS checks the text while typing and not till user actually taps search in the search bar!
SearchView searchView = (SearchView) findViewById(R.id.Search_View);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
///Toast.makeText(getApplicationContext(),query,Toast.LENGTH_SHORT).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getApplicationContext(),newText,Toast.LENGTH_SHORT).show();
return false;
}
});
so when I type in searchbox, the message toasted letter by letter while I'm typing and not let me click on search on keyboard.
I searched a lot and couldn't find a solution.
Thanks for your solutions... :)

If you want to get text when press button from key board then you use:
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();
return false;
}
query is your text where you want to use.
If you want get text with other button clicks like Button, TextView etc..
then you use like:
buttonname.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String query = searchView.getQuery();
}
});

SearchView is supposed to give users suggestion while typing. If you need to serch entire sentence or word consider using a EditText with a button instead of SearchView. Set listener to the button and start action (Toast the message) after user tap it.

Try set method onQueryTextChange(String newText) empty body and return true if you do not want the search view to perform default action. Handle the input text yourself only in onQueryTextSubmit(String query).

Related

Hiding Cardview until search is initiated

I've followed the code on http://www.camposha.info/source/android-recyclerview-search-filter and made my search filter work. The only thing I need is when opening the activity to keep the Cardview hidden, or invisible until the search has started in the searchbar (in other words, to keep the Cardview hidden until I start typing something in the searchbar).
I have read the comments which I should have edited the Edittext but I don't have it. what should I do?
Keep the cardview's visibility gone in the layout. And add a queryTextListener to your searchView
yourSearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if(newText.length()>0){
yourCardView.setVisibility(View.VISIBLE);
}else{
yourCardView.setVisibility(View.GONE);
}
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
}

How can I hear when the user presses the submit button on a SearchView but the user has not entered any text

Hello I am trying to know when the user hits the search/submit button on a searchView but they have not entered any text. I am aware I can use setOnQueryTextListener but this will only get called if the user enters some text. If you have any ideas please let me know. Thank you!!
verify the length of the text entered like this:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String key) {
if(key.length()>2){//if the text to search more then 2 caracter
//user hit the search button, do your search
}
return false;}
#Override
public boolean onQueryTextChange(String s) {
// user is typing ...
return false;
}
});

SearchView does not respond to clicks after query submit

My app contains a standard SearchView widget. As you can see from the code below, I am setting an OnClickListener, an OnCloseListener, and an OnQueryTextListener.
If I tap the SearchView, it initially responds as expected. If I enter text and press the search button the keyboard, the OnQueryTextListener fires correctly, and the keyboard is dismissed as per searchView.setIconified(true). However, if I now tap the SearchView again, the OnClickListener is not fired. The keyboard still appears and the field becomes editable, but my code in the OnClickListener is not executed.
If I use the "X" icon to close the search view after this, everything returns to normal. The next time I click on the SearchView, my listener is fired.
I have additional code that I'll need to execute every time the SearchView is clicked.
What could be causing the listener to not fire in this specific instance? Is there something else that I should be doing in OnQueryTextSubmit?
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
filterLayout.setVisibility(View.VISIBLE);
searchView.setIconified(false);
System.out.println("on click");
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
filterLayout.setVisibility(View.INVISIBLE);
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchView.setIconified(true);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
I found a half-answer to this problem. If I clear the focus of the searchView when the query is submitted, then when the user next taps the searchView, the OnQueryTextFocusChangeListener gets a call. It doesn't exactly answer my original question, but it's an acceptable workaround for my case.

Custom searchview in actionbar

I have a searchview in my application. I need to search some information when user writes some criteria and after this, need show result in ListView.
How is it possible to know, if the user pressed the search button on the keyboard or not?
I read about OnQueryTextListener, but I still can't understand how to handle the press of a button from the android keyboard.
sv is the searchView
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
//here is what you want
return false;
}
#Override
public boolean onQueryTextChange(String s) {
// here you write what to do while typing
return false;
}
});

Searchview - Hide toast that appears by pressing key

I'm using a filtering SearchView. Everything works fine but, when you leave the keyboard to type and press a button, a kind of toast is shown. Anyone know how to remove it?
My code is:
searchView = (SearchView) view.findViewById(R.id.buscador_lineas_transporte);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){
lvLineasTransporte.clearTextFilter();
}
else{
lvLineasTransporte.setFilterText(newText);
}
return true;
}
});
Thanks.
Solved
This keyboard appears when you implement the interface Filterable in your adapater.
I recommendto use "AutoCompleteTextView" instead of "SearchView".
AutoCompleteTextView searchView = (AutoCompleteTextView) view.findViewById(R.id.searchView);

Categories

Resources