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;
}
}
Related
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).
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.
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;
}
});
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);
I want to disable certain features of my app while the user is entering text for a search. The xml for the relevant item in my ActionBar is
<item android:id="#+id/actionbar_search"
android:orderInCategory="1"
android:showAsAction="always|withText|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/earth_2508858_search_en"
android:inputType="textPostalAddress" />
and in the corresponding code that I have at present to cater for the search is
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
MenuItem DestinationTxt = menu.findItem(R.id.actionbar_search);
final SearchView mySearchView = (SearchView)DestinationTxt.getActionView();
mySearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) { return false; }
#Override
public boolean onQueryTextSubmit(String query) {
//Hide the Keyboard
imm.hideSoftInputFromWindow(mySearchView.getWindowToken(), 0);
// CODE TO DO THE SEARCH
return true;
}
});
}
I've browsed the methods on SearchView, but I didn't see anything that would tell me whether it's active or not. I'm also worried about putting in a boolean state variable to indicate when the text is being typed into the SearchView, in case some behaviour that I haven't catered for occurs (e.g. back button pressed, activity gets suspended), and somehow the state variable gets stale so that the disabled features stay disabled. So I'm looking for a robust way of doing this, all help appreciated :-).
Update. An answer below suggests using the interface OnFocusChangeListener which is implemented by the mySearchView object, and/or the mySearchView.isFocussed() method. Both sounded promising, however I've now tested and neither seem to work. Perhaps their failure has got something to do with the fact that this SearchView is in the ActionBar? In any case, I'm still after a robust solution.
It's right there.
mySearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) { return false; }
That's where you'll get updates to text changes in the SearchView.
The return value should be as such (documentation):
Returns
false if the SearchView should perform the default action of showing any suggestions if available, true if the action was handled by the listener.
If you want to know if the SearchView has been activated or deactivated, use View.setOnFocusChangeListener(View.OnFocusChangeListener);
public interface OnFocusChangeListener{
public void onFocusChange (View v, boolean hasFocus);
// The boolean will tell you if it's focused or not.
}
Since monitoring the focus didn't work, I looked at the SearchView documentation again. It's a bit convoluted, but it seems like the intended solution to this problem.
If your SearchView is inflated from a menu XML in onCreateOptionsMenu(), then you can add this line:
menu.findItem(/* your SearchView's ID here */).setOnActionExpandListener(
new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse (MenuItem item){
enableInteraction();
return true; // Allow the SearchView to collapse.
}
#Override
public boolean onMenuItemActionExpand(MenuItem item){
disableInteraction();
return true; // Allow the SearchView to expand.
}
}
);
Then enable and disable your Activity's views in enableInteraction() and disableInteraction(), respectively. You should retain the MenuItem in your Activity so you can query it in onResume() like so:
#Override
public void onResume(){
super.onResume();
searchViewMenuItem.isActionViewExpanded() ?
disableInteraction() : enableInteraction();
}
This part might not be needed. The SearchView might automatically get collapsed when the Activity is hidden and stay that way, so you can simply call enableInteraction() in onResume() so your user isn't locked out.
If you just need to reference the state of the SearchView, use
searchViewMenuItem.isActionViewExpanded();