I'm trying to add functionality to the Search Widget Back Button. Right now, it only closes the search bar. I also want it to close the search list and return the original list before the search.
Search Widget Back Button Image
Attached is my search code.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
winelist = dbHelper.searchWineList(query);
adapter.clear();
adapter.addAll(winelist);
adapter.notifyDataSetChanged();
}
}
there's a listener for SearchView which listens to the closing of search, have you tried having a callback there to reset the whole list?
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 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 -
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)?
I am trying to create an action bar with search.
I am able to create the search bar. How I can get the text entered in the field to string? Is there any good code for action bar with search?
What I done is
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String search = intent.getStringExtra(SearchManager.QUERY);
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);
}
Now I have this search bar up with code below.
Link : http://pastebin.com/6vSBR8iX
How do I let it become the type of search window like the market has?
This is the Android manifest I have.
Link : http://pastebin.com/ue6NSTCr
Someone please help me out with this. I have tried
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
But could not get the search up.
Have you followed the guide here: http://developer.android.com/guide/topics/search/search-dialog.html#PerformingSearch
If you have created Activity and Action Bar Widget, I hope you did not forget to add:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
And to clarify your question, could you please post more info?
Cheers!