I want to add search history to my SearchView. I found this question. It is rather useful but it looks like I can't handle it this way. First of all I use only fragments in my app while it looks like the solution above requires to create new activity for making search (well maybe I can reuse the same activity using some Intent flags). Also I can't provide search hint bia string resources.
I have fragment with SearchView in actionbar. When user enters search query I just perform web request and display results in the same fragment. I only need to add search history functionality to it. Can this be done completly programmatically?
Related
I am building an app which contains a signle Activity which contains an ActionBar. The activity contains a tab host with 4 tabs and each can be searched for something else, depending on the tab and fragment that is displayed.
According to the developer guide (https://developer.android.com/training/search/setup.html#add-sv), when pressing the search button after the user has entered his query, the activity will be launched with a search intent in which I can read the query like this:
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
However, I don't want to relaunch the activity, then decide which fragment raised the query, set the tabs to the correct one, constructing a new fragment and so on... What I want to do is simply read the query itself inside the fragment which the user type in (I noticed that the SearchView class got a getQuery method).
How can I somehow implement a listener that reads the query after the user has finished writing his query and pressed the search button in the virtual keyboard without recreating the activity? I want to just get a reference to the search box and use it like it's a TextView, read it's text and use it.
What I eventually did was implementing OnQueryTextListener. This listener got 2 methods: onQueryTextChange which is called on every key pressed with the text as the parameter, and onQueryTextSubmit which is invoked when the user presses the search button. If you return true in onQueryTextSubmit, it means that everything is handled and there is no need to launch an intent.
I'm using a ContentProvider for search suggestions but I have a problem for handling a suggestion click: I explain the search works good but when I click in suggestion I go to the search activity but the re isn't anything, so how can I handle a click suggestion?
Your XML\searchable.xml (or equivalent) should include
android:searchSuggestIntentAction = android.intent.action.VIEW
otherwise your intent won't be called.
http://developer.android.com/guide/topics/search/searchable-config.html defines this field as:
The default intent action to be used when a user clicks on a custom
search suggestion (such as "android.intent.action.VIEW"). If this is
not overridden by the selected suggestion (via the
SUGGEST_COLUMN_INTENT_ACTION column), this value is placed in the
action field of the Intent when the user clicks a suggestion.
I'm making a function in my android app that, at a home screen the user has the choice to view a league or choose a team. I want to know if its possible to and how to store their team selection so that it doesn't go to that activity the next time they start the app.
Also, how would I implement a menu button to change that team if they so wish?
SharedPreferences will allow you to store your league/team selection. You can retrieve this when you start your application and move to your next activity if it is set.
Look how to create Menu items in Android here. These are straight forward and very easy to implement. Just have that menu button launch your "select a team/league" activity
This should be something trivial that you should have been able to google:
For persisting simple data you can use SharedPreferences
I have a search feature with a list view. When the user click on search and filters the result the activity is loaded from the beginning. So here is the flow.
User opens my application (Gets the whole list ot items) -> Search1 (Filtered search) -> Search2 (Filtered result 2) -> Search3 (Filtered result 3). Now when the user clicks back he goes to Search 2 -> then back to result 1 -> then back to the mail list.
This is how is my application working now. I want this to change as when users is on any search result I want him to go back to main list without going back to any search results.
Basically I dont want android system to track the search results so that when user clicks back, he should directly go to the main list.
How do I do this. Please advice if you have any alternative for this. Thank you for your advice and time. Thank you.
You should set your search activity to be android:launchMode="singleTop" in your manifest, that way there's a single search activity and you're only responding to search intents within it. This is actually the "ideal" behavior according to the docs.
When you are done with the search results you could finish that activity. You might even be able to use finishOnTaskLaunch to achieve the same thing and it is probably more desirable too.
My application shall work the following way:
Activity #1 contains a text field (EditText) and a button.
If the user clicks the button a search dialog is opened via onSearchRequested().
This calls the searchable activity #2 which extends ListActivity. It provides a list of items via setListAdapter().
If the user clicks on a list item activity #2 shall pass the selected item's text back to activity #1 and display it in the text field.
Bullets #1-3 are clear and working. However I don't have any idea how to implement #4. I know about the possibility to use intents but it doesn't work if I use an intent after onSearchRequested().
Thanks,
Robert
The following solution is working fine for me:
http://blog.dpdearing.com/2011/05/getting-android-to-call-onactivityresult-after-onsearchrequested/
I would simply send an intent with your selected item as extra (putExtra) to your activity#1 (since the search dialog is between activity#1 and activity#2, you can not use startActivityForResult to post back the result to activity#1)
If the search dialog is in activity#1, then you can use startActivityOnResult (thanks dmon)
Simple, quick fix: store the data statically and do a check to retrieve it in Activity #1's onResume().