I have an Activity with a ListView and clickeable items to show details of each of them in a separate Activity. My main activity has a SearchView to filter those items. I want to be able to click on an item (that open's a new activity) and when i press back button, my searchView remains same (with my filtered list).
I implemented my searchview like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
Log.w("SEARCH", "SearchOnQueryTextSubmit: " + query);
mQuery = query;
if (!mSearchView.isIconified()) {
mSearchView.setIconified(true);
}
searchItem.collapseActionView();
return false;
}
#Override
public boolean onQueryTextChange(String s) {
Log.w("SEARCH", "SearchOnQueryTextChanged: " + s);
mQuery = s;
contactAdapter.getFilter().filter(s);
return false;
}
});
And i was trying to implement something into onResume or on the same onCreateOptionsMenu to check if mQuery had something.
if (!TextUtils.isEmpty(mQuery)) {
final String thisQuery = mQuery;
mSearchView.post(new Runnable() {
#Override
public void run() {
Log.d(TAG, "onResume: setting mQuery to searchView");
Log.d(TAG, "mQuery: "+thisQuery);
MenuItemCompat.expandActionView(searchItem);
mSearchView.setQuery(thisQuery, true);
}
});
}
Tried on both methods but with no success. On onCreateOptionsMenu, if i debugg it, SearchView is called with my query to be filtered, but right after that, another call comes with an empty search! I don't know where this comes from.
Any help? Thanks!
I am glad this could help:
What you need is How to maintain the previous state of an activity. Check the selected answer here -
Related
I have SearchView which I want to programmatically expand and set text when the device orientation change. I tried many found solution but nothing work. If I use only setQuery then my list is filtered, but when I use also expandActionView then the search view does not contain the given search text and onQueryTextChange is called twice, first with the given text and the second time with the empty text.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.activity, menu);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setIconifiedByDefault(true);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mCurrentQueryString = null;
if (mAdapter != null) {
mAdapter.clearFilter();
mStopSearching = true;
}
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
if (mAdapter != null) {
mStopSearching = false;
}
return true;
}
});
mSearchView = searchView;
mSearchMenuItem = searchMenuItem;
mSearchView.post(new Runnable() {
#Override
public void run() {
MenuItemCompat.expandActionView(mSearchMenuItem);
mSearchView.setQuery(mCurrentQueryString, true);
}
});
super.onCreateOptionsMenu(menu, inflater);
}
I use the search view in the fragment which is used in the view pager. The fragment is retained. The problem is that onQueryTextChange function is called twice, by the second time with the empty text, but I am not sure why. This function is also called with the empty text when I first time open the search view when the fragment is initialized, but when close the serach view and open it again, this function is not called.
How can I fix it?
I've also faced that issue. The solution I've come up with was via posting with handler.
#Override public boolean onPrepareOptionsMenu(Menu menu) {
...
// SearchView doesn't retain it's state after orientation change
// have to handle it the bad way (╯°□°)╯︵ ┻━┻
boolean isQueryExists = !TextUtils.isEmpty(mSearchQuery);
if (isQueryExists) {
// Calling directly doesn't take effect
// Custom runnable class in order to refrain from context leakage
new Handler(Looper.getMainLooper()).post(new SearchMenuRunnable(mSearchView, mSearchQuery));
}
...
}
SearchMenuRunnable.java
public class SearchMenuRunnable implements Runnable {
private WeakReference<SearchView> mSearchViewWeakReference;
private String mSearchQuery;
public SearchMenuRunnable(SearchView searchView, String searchQuery) {
mSearchViewWeakReference = new WeakReference<>(searchView);
mSearchQuery = searchQuery;
}
#Override public void run() {
if (null != mSearchViewWeakReference.get()) {
SearchView searchView = mSearchViewWeakReference.get();
searchView.setIconified(false);
searchView.setQuery(mSearchQuery, true);
searchView.clearFocus();
}
}
This are source from one of projects in github. You can examine them here, it's pretty simple project.
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.
The text i enter in searchView disappears on config change.
I am already handling the config change but still its not working.
As i have used fragments, so below code is written inside fragment.
Please solve my problem with respect to that.
My problem is i am not able to retrieve savedInstance in onActivityCreated method
Below is my code:
Snippets of useful code
private String searchQuery="";
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
searchQuery = savedInstanceState.getString(Tag.SEARCH_QUERY);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(Tag.SEARCH_QUERY, searchQuery);
}
#Override
public void onCreateOptionsMenu(Menu menu,MenuInflater menuInflater) {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_search, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = null;
if (menuItem != null)
searchView = (SearchView) menuItem.getActionView();
if (searchView != null) {
searchView.setQuery(searchQuery,false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
//searching is done in async task
searchQuery = query;
NetworkUtility.onProgressBarShow(getActivity());
MyAsyncTaskDownloadDetails myAsyncTaskDownloadDetails = new MyAsyncTaskDownloadDetails();
myAsyncTaskDownloadDetails.execute(new String[]{Tag.PLP_URL + query, Tag.PLP,""});
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return true;
}
});
}
super.onCreateOptionsMenu(menu, menuInflater);
}
}
First of all you are saving queryText in onQueryTextSubmit callback which means searchQuerywill remain empty unless and until user have submitted query for search, so keep that in mind.
Secondly try following code. It will work I have tested it.
if(searchQuery.length()>0){
//See this commented code
// if(Utils.hasIceCreamSandwich()) {
// searchMenuItem.expandActionView();
// }else {
// MenuItemCompat.expandActionView(searchMenuItem);
// }
MenuItemCompat.expandActionView(menuItem);
searchView.setQuery(searchQuery, false);
}
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
I'm using this for software search (search that collapses in the menu when the user presses the magnifying glass) and it works perfectly :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_entries, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.viewMenu_search).getActionView();
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) searchView.findViewById(id);
textView.setTextColor(Color.YELLOW);
textView.setHintTextColor(Color.WHITE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return true;
}
#Override
public boolean onQueryTextChange(String s) {
ViewEntries.this.customAdapter.getFilter().filter(s);
return true;
}
});
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
My question is : how do I get the search query string (the one like the parameter String s) when the user starts the search using the hardware button. I tried this, but the intent I get is not the search intent, but the one that started the activity (so I never enter the if):
#Override
public boolean onSearchRequested() {
Intent intent1 = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String s = intent1.getStringExtra(SearchManager.QUERY);
ViewEntries.this.customAdapter.getFilter().filter(s);
}
return super.onSearchRequested();
}
I also say when debugging on the emulator that onSearchRequested is called the moment you start the search, not after you enter the query. Maybe a hint on how to get the searchview there (id or something)?