Searchview hide Toast message in android - 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;
}
});

Related

adapter.getFilter().filter(newText); not triggered and not filtering anything

The problem is when I change my simpleAdapter to CursorAdapter my SearchView doesn't work anymore. I am trying to debug using logcat but even in logcat no logs happen so I wondering why what's the problem of my code since the logcat doesn't show errors.
here is my codes
SearchView searchView = (SearchView) item.getActionView();
searchView.setQueryHint("Search Title/Artist");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
Calling the CursorAdapter
Cursor res = db.query("songs",null,null,null,null,null,"title");
adapter = new ClientCursorAdapter(MainActivity.this,res, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
////
////
lv.setAdapter(adapter);
Calling the CursorAdapter and display it into my listview works well but when I'm trying to search in Searchview nothing happen, no changes happen to the listview.
one more thing.
I put the Toast inside the onQueryTextChange like this
#Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(MainActivity.this, newText, Toast.LENGTH_SHORT).show();
return false;
}
The Toast displayed what I'd typing in Searchview. so I don't know what actually happen to my searchview. please help.

How to get a text from SearchView?

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;
}
});

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();
}

submit a query when click on search button in keyboard

I have search view in my fragment. when I click on it , keyboard is open and I can type text. I want when I click on search button in keyboard , my query send to my server and get result but I don't know how get search event. any solution?
You have to extend OnQueryTextListener, attach the listener and implement onQueryTextSubmit.
Example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
searchView = (SearchView) menu.findItem(R.id.mActionSearch).getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
Pozzo Apps Answer is right
but for api below 11 and compat library you can use something like this :
MenuItem search_menu=(MenuItem)menu.findItem(R.id.action_search);
SearchView searchView =(SearchView)MenuItemCompat.getActionView(search_menu);
You can also apply setOnKeyListener on search view like as below:
searchview.setOnKeyListener(new View.OnKeyListener(
{
Public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
Case KeyEvent.KECODE_ENTER:
// Apply action which you want on search key press on keypad
return true;
default:
break;
}
} return false;
}
});
You have to add new OnQueryTextListener, and implement onQueryTextSubmit. This also works in a fragment.
Example:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_search, menu);
SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//Do something here
Toast.makeText(getActivity(), "Search: " + query, Toast.LENGTH_SHORT ).show();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
super.onCreateOptionsMenu(menu,inflater);
}

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