What's the best approach in Android to get a searchview with autosearch results styled like this example from the Uber app? I especially like how the results are displayed directly on the screen and not with a dropdown look.
I was thinking possibly getting the query of a search and add the results on the screen dynamically as buttons but there must be a simple way to achieve the style and functionality they have here. Open to any suggestions.
I just took a look at the Android's Uber app, and there they have an EditText on the top with a ListView below that. Whenever the user types in anything in the EditText, the query is made to the server to get the data from it, and the results are displayed in the ListView below.
You should make the query to the server in afterTextChanged() method of the EditText TextWatcher. Also, make sure when the user is typing in continuously, cancel the previous request and only the latest char change query string should go in the request.
Related
I have followed this tutorial and have successfully implemented a recylerview with searchview in my app https://github.com/Wrdlbrnft/Searchable-RecyclerView-Demo. However, now instead of having text in my recylerview I have icons representing two states for a user Online and Offline, what I want to do is when the user types in Online in the searchView I only want the Online icon to show up and simiarly when they type Offline I only want the offline icon to show up.
To filter text, as shown in the example I have done this;
if (personStatus.contains(query)) {
filteredModelList.add(model);
}
This works perfectly for text based results in my recylerview, however, I want to filter based on icons in the recylerView.
The icon can either be R.drawable.Online or R.drawable.Offline. I have a imageView called icon.
What I have tried so far;
if(query.toLowerCase().equals("online") {
filteredModelList.add(model.getIcon().getDrawable(R.drawable.online));
}
This doesn't seem to work, I get a error on getIcon, it says cannot resolve method getIcon. I do have a method getIcon as shown in the example above in the exampleModel class;
what I want to do is;
IF searchView text = "Online" THEN
Only show recylerview items that contains icon Online
I hope I have explained myself fully, if not please let me know.
The approach you are using might be slow when dealing with large data set, because you are doing the search on the main thread. If the searching does not complete in 8ms you are about to experience junk.
Comparison that you are using query.toLowerCase().equals("online") is fine. You need to move that logic inside your adapter that should implement Filterable. You need to toggle that online/offline status in your model, like: setStatus(Status.ONLINE) or setStatus(Status.OFFLINE) so iteration to find all items for particular status is easy, only with using your condition & constraint from the SearchView, i.e query.toLowerCase().equals("online").
Take a look at my answer here. It is more or less I mentioned.
i have SearchView on the ActionBarand used setSuggestionAdapteron it to set the adapter for autocomplete.
problem is that when i type chars into the search view a popup list of suggestion does show but does not get filtered according to the string im typing, i always get the full underlying list in the adapter, is this the expected behaviour?
Yes, this is the expected behavior. By setting a custom search adapter you are telling the SearchView that you want to override the default suggestions behavior and provide your own list of suggestions to the user.
This is useful in a number of situations, such as if you want to display the user's search history as a suggestion.
If you want to use the built in querying system for creating suggestions based on what the user is typing, you should create a search interface as described in the Creating a Search Interface documentation.
how can I create in my android app a button that will automatically put the search suggestion into the search bar, so that when you click on the button, the search bar will be automatically filled with the corresponding text?
An example is shown in the image below.
Have you looked into AutoCompleteTextView? From the docs:
The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
EDIT: If you're using a SearchView, the process is much different. Assuming you've already set up the SearchView, you have to provide search suggestions that the system will display as the user types. These can be recent query suggestions or custom search suggestions.
There are several steps involved, but in a nutshell, you will have to create a Content Provider that takes a search query and serves up a list of suggestions (details here). Once you've done this, you can configure your search bar to fill itself in when the user selects a suggestion (details here).
Hope this helps!
The solution is to call setQueryRefinementEnabled(true) on the SearchView object
Even a Snippet of an android code to Integrate Google Search into an Edit text will be really useful, according to my requirement i don't want the google search data or whatever but only the dropdown so that it will b easier for people to type and don't have to waste time :)
You can see about the componenent is AutoCompleteTextView
An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
I am new to android. I was stuck on a problem but I finally solved it.
I was using a TextField instead of CompleteTextViewField so whats the difference between these two and when should I use each one of them?
Thanks
Neither of those classes you mention (TextField, CompleteTextViewField) exist. Do you mean EditText and AutoCompleteTextView? I think the documentation explains it pretty well:
[AutoCompleteTextView is] An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
That is, use it rather than a normal EditText if you have a set of common autocompletions for what gets entered in the box. The docs also link to a full sample that shows how to populate that list of suggestions with an Adapter.