Trying setOnQueryTextListener in searchView - android

I want to create a searchview that when the user typing , a table will be created.
From a little search I understood I need to use this method : setOnQueryTextListener.
I get an error in this line :
search.**setOnQueryTextListener**(new OnQueryTextListener() {
with the message:
The method setOnQueryTextListener(SearchView.OnQueryTextListener) in the type SearchView is not applicable for the arguments (new SearchView.OnQueryTextListener(){}).
Thanks !
public boolean onCreateOptionsMenu(Menu menu)
{
final TableLayout table = (TableLayout) findViewById(R.id.restTable);
getMenuInflater().inflate(R.menu.action_data,menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.action_search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String namelike) {
if(namelike.length()>0)
{
dataHandler.getRestLike(namelike);
createTableLayout(table);
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return true;
}
});
return true;
}

Related

Not able to get the text entered in search bar in android

I am trying to implement search functionality for my android application. I am not able to get the text entered in the search bar. I have tried all the methods mentioned in SO. Please let me know what mistake am I doing. I am struggling from a long time.
my code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_activity_for_fragments, menu);
SearchManager searchManager = (SearchManager)
getActivity().getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getActivity().getComponentName()));
enteredText = (String) searchView.getQuery();
Log.d("iFocus", "" + enteredText);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("iFocus", ""+searchView.getQuery());
if (0 != searchView.getQuery().length()) {
String spnId = searchView.getQuery().toString();
setSearchResult(spnId);
} else {
setData();
}
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
Log.d("iFocus", ""+newText.toString());
adapter.getFilter().filter(newText);
return true;
}
});
}
All suggestions are welcome. Thanks in advance.
Why don't you use in the overridden method onQueryTextSubmit the parameter that you become to get the query like this:
#Override
public boolean onQueryTextSubmit(String query) {
Log.d("iFocus", query);
if (0 != query.length()) {
setSearchResult(query);
} else {
setData();
}
return true;
}
See my example project with ToolBar and search menu item, min API level = 7:
android-toolbar-search

How to code for SearchView on ActionBar in android

I am not able to get the filter value from the list.
For the wrong search the list shows blank page, when I enter the right value, only the first value from the list is fetched.
After implementing OnQueryTextListener to my class, I inserted the following code:-
Please help. Thanks in Advance
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){
lv.clearTextFilter();
}else{
lv.setFilterText(newText.toString());
}
// TODO Auto-generated method stub
return true;
}
Implementing Filterable in Adapter class
public Filter getFilter(){
return null;
}
I Added the above method, now there is NullPointerException generated.
Check out this tutorial it uses Sherlock Action Bar, it shows exactly what you are trying to do. I have used it and works great. http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/

Android Actionbar SearchView

I have tried the actionbar SearchView in android.
Unable to get the entered string from the below code please anyone help?
try changing onQueryTextSubmit code to:
public boolean onQueryTextSubmit(String query) {
if (query == null) {
Toast.makeText(getApplicationContext(), "Null Query", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Query is:" + ""+query, Toast.LENGTH_SHORT).show();
}
}
see this
You can use the search bar as like this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView();
if (null != searchView) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
return true;
}
public boolean onQueryTextSubmit(String query) {
//Here u can get the value "query" which is entered in the search box.
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}

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

force iconified on SearchView

I have a SearchView in my actionbar now it works exactly as it should , any way i have one problem , when i click the submit button i.e the query is submitted and the results appear , when i return back to the activity from which i start the search from the SearchView is expanded , my question how can i close it i.e iconified it on submitting . this is my code .
searchView = (SearchView) item.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setIconified(true);
searchView.setQueryRefinementEnabled(true);
searchView
.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
try {
searchView = (SearchView) item.getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setIconified(true);
searchView.setQueryRefinementEnabled(true);
searchView
.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
try {
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
}); // doesnot work
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
});
For API>=14 you can use item.collapseActionView().
This solution works:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.home_screen, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchMenuItem
.getActionView();
searchView.setIconifiedByDefault(false);
if (searchManager != null && searchView != null) {
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}// end if
if (searchView != null) {
searchView.setQuery("", false);
}// end if
}// end if
}
});
searchView
.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure
* keyboard disappears when query is submitted
*/
if (searchView != null) {
searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
I took this from another question and it seemed to work for me.
#Override
public void onStart()
{
if (mSearchView != null)
{
getSearchMenuItem().collapseActionView();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
//get your search view
msearchMenuItem = menu.findItem(R.id.search);
}
public MenuItem getSearchMenuItem() {
return msearchMenuItem;
}
it closes the searchview every time you hit the back button (which makes the activity go into onStart())

Categories

Resources