How to get a text from SearchView? - android

I need to get a text from SearchView and compare it to strings in my activity's ListView and show a Toast if the word in a SearchView is in my ListView. How do I do that? Here's my working code for the SearchView:
MenuItem ourSearchItem = menu.findItem(R.id.menu_item_search);
SearchView sv = (SearchView) ourSearchItem.getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});

To get text of SearchView, use searchView.getQuery().

try this code :
MenuItem ourSearchItem = menu.findItem(R.id.menu_item_search);
SearchView sv = (SearchView) ourSearchItem.getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getContext(), query, Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
}
return false;
}
});

Related

Try Everywhere but not getting satisfactory answer about SearchView in RecyclerView

This is my OnCreateOptionMenu
An error is showing in setOnQueryListener
here contact_object is model class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.search_list,menu);
final MenuItem myMenuItem=menu.findItem(R.id.search_contact);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem (R.id.search_contact) .getActionView ();
changedSearchViewTextColor(searchView);
// ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (!searchView.isIconified()){
searchView.setIconified(true);
}
myMenuItem.collapseActionView();
return false;
}
}
#Override
public boolean onQueryTextChange(String newText) {
newText=newText.toLowerCase();
final List<Contact_Object> filtermodelist=filter(contacts,newText);
adapter.setFilter(filtermodelist);
return true;
}
});
return true;
}
I have implemented the filter option as below
public List<Contact_Object> filter(List<Contact_Object> p,String query )
{
query=query.toLowerCase();
final List<Contact_Object> filterModeList=new ArrayList<>();
for (Contact_Object model :p){
final String text=model.getName().toLowerCase();
if (text.startsWith(query)){
filterModeList.add(model);
}
}
return filterModeList;
}
I have declared the setFilter function as follow in CustomAdapter class
public void setFilter(List<Contact_Object> listItem){
parentList=new ArrayList<>();
parentList.addAll(listItem);
notifyDataSetChanged();
}
I have tried hard but didn't get the appropriate answer
please anyone help me,waiting for answer
There is no logic in onQueryTextChange because newText is not passed anywhere. You are passing the empty list to the setFilter method.
#Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
final List<Contact_Object> filterModeList=new ArrayList<>();
for (Contact_Object obj : p) {
String name = obj.getName().toLowerCase();
if(name.contains(newText))
filterModeList.add(obj);
}
adapter.setFilter(filtermodelist);
return true;
}

SearchView :Triggering onQueryTextChange on Collapsing of the searchView

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
I don't want search(newText) to be executed when searchview collapses.
I tried
if(searchView.isShown) search(newText);
and
if(!searchView.isIconified) search(newText);
but when I close the searchview, searchView.isShown and searchView.isIconified values will be updated after triggering onQueryTextChange.
My onCreateOptionMenu code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_contacts, menu);
addContactMenuItem = menu.findItem(R.id.action_add_contact);
searchViewMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchViewMenuItem);
MenuItemCompat.setOnActionExpandListener(searchViewMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
addContactMenuItem.setVisible(false);
if (contactListAdapter != null)
contactListAdapter.getFilter().filter(searchView.getQuery());
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
addContactMenuItem.setVisible(true);
contactListAdapter.performGroupBasedFiltering(groupSpinner.getAdapter().getItemId(groupSpinner.getSelectedItemPosition()));
return true;
}
});
return true;
}
Please help me out! Thanks in advance.
Try this use setOnActionExpandListener
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
// perform your action here
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
// perform your action here
return true;
}
});
return true;
}
EDIT
as per your comment
I need search(newText) not to be executed on collapsing.
than set this searchView.setOnQueryTextListener(null); when you don't need

Searchview hide Toast message in android

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?
this is a my code:
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;
}
});

Regarding-SearchView in Actionbar

I implemented a searchView in Actionbar
final SearchView searchView = (SearchView) menu.findItem(
R.id.search_item).getActionView();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String arg0) {
return true;
}
#Override
public boolean onQueryTextChange(String arg0) {
return false;
}
});
What i wanted is that after submitting the text , the searchView should become readonly.
i have tried searchView.setEnabled(false); but didnt get ..Please help me
In your onQueryTextSubmit() Method: add this
searchView.setInputType(InputType.TYPE_NULL);
searchView.invalidate();
and to remove it use this:
if(searchView.getInputType()==InputType.TYPE_NULL) {
searchView.setInputType(InputType.TYPE_CLASS_TEXT);
searchView.invalidate();
}

Collapse searchview after submit

I am using searchview in my application ( without action bar). How can I collapse searchview after query text submit?
I have these listeners ;
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager)thisFr.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(globalSearch.getWindowToken(), 0);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
I don't use ActionBar so I don't have a function like collapseActionView().
you need to call setIconified(true) twice to actually collapse your search view, with first call text is cleared with second call keyboard and search view get closed.
You can do it this way in your activity, tested with actionbarsherlock (it even hides the keyboard, make sure to return false in onQueryTextSubmit):
private MenuItem searchMenuItem;
public MenuItem getSearchMenuItem() {
return searchMenuItem;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// ...
searchMenuItem = menu.findItem(R.id.menu_search);
// ...
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
MenuItem searchMenuItem = getSearchMenuItem();
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// ...
return true;
}
});
// ...
return super.onCreateOptionsMenu(menu);
}
If you are using the SearchView in the OptionsMenu, you ca call invalidateOptionsMenu()
//close suggestion list on query text submit
searchView.setIconified(true);
final MenuItem searchterm = menu.findItem(R.id.search);
SearchView searchView = null;
searchView = (SearchView) searchterm.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
searchterm.collapseActionView();
}});

Categories

Resources