Not able to get the text entered in search bar in android - 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

Related

My app doesn't load toolbar options

I'm doing a search bar, but I found a problem, because if I put the code at the end of MainActivity it doesn't load options.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
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 SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
return false;
}
});
}}
What should I do? Did I do something wrong?
Thanks in advance
Your logic is after the return statement, hence, never reached. Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
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 SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
return false;
}
});
}
return super.onCreateOptionsMenu(menu);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
That is never reached, your return statement is before that. The rest looks fine.

Android: How to implement a SearchView with sections so as to search across multiple lists

I am trying to implement a searchView in my app that enables the user to search a text across multiple lists. Something like below:
So far I have been able to implement a searchView that searches for text across a single list and it works great, but I can't think of a way to make it work across multiple lists with the section headers showing.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
item = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(item);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSuggestionsAdapter(mAdapter);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
#Override
public boolean onSuggestionSelect(int position) {
return false;
}
#Override
public boolean onSuggestionClick(int position) {
// some code here
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String query) {
populateAdapter(query);
return true;
}
});
// return true;
return true;
}
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "cityName", "country"});
for (City city: list) {
if (city.getTitle().toLowerCase().startsWith(query.toLowerCase()))
c.addRow(new Object[]{city.getID(), city.getTitle(), city.getNation()});
}
mAdapter.changeCursor(c);
}
Any help would be appreciated, Thanks !!
You can use this library to have different sections in your RecyclerView (movies, concerts, venues). Then you just need to filter the data of the sections in your onQueryTextChange method.
There is a full example here.

Trying setOnQueryTextListener in searchView

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

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

Categories

Resources