I am currently using SearchView
widget inside ActionBarcompat
to filter a list while searching.
When the user starts entering text the ListView in the main layout updates with an Adapter to filter the results. I do this by implementing a OnQueryTextListener
and filter the results on each key stroke.
Instead, I want to create a Gmail like search box with auto suggest list generated and no changes to the underlying view
I have went through this tutorial that uses the SearchView
component but it requires a searchable activity. I want the drop-down to be over the MainActivity where I have the ListView (like in the Gmail app) and not a dedicated Activity.
Besides, implementing it the same way as in the tutorial seems like an overkill for what I want (just a dropdown)
If you just want a component that does what is discribed in the question, I suggest this library. You can also implement the out-of-the-bx searchable interface, however, be aware that it does have UI limitations:
To implement an interface similar to Gmail App, you will have to understand conceps of:
Final result should look something like:
There are many (many) ways to get to the same result (or better), I'll discribed one possible way.
Part 01: Layout
I decided to manage the entire interface in a new Activity, for that I've created three XML layouts:
custom_searchable.xml: assemblys all UI elements in one RelativeLayout that will serve as content for the SearchActivity;
custom_searchable_header_layout.xml: holds the search bar where the user will type his query. It will also contain the mic, erase and return btn;
custom_searchable_row_details.xml: holds the UI elements to be displayed in the result list to be displayed in response to the user query;
Part 02: Implementing the SearchActivity
The idea is that, when the user types the search button (which you can place any where you want), this SearchActivity will be called. It has some main responsabilities:
Bind to the UI elements in the custom_searchable_header_layout.xml: by doing that, it is possible:
to provide listeners for the EditText (where the user will type his query):
add listener for the return button (which by its turn will just call finish() and return to the caller activity):
calls the intent for google speech-to-text API:
Content Provider
When building the a search interface the developer has tipically two options:
In both cases the answers shall be delivered back as a Cursor object that will have its content displayed as itens in the result list. This whole process can be implement using the Content Provider API. Details about how to use Content Providers can be reached in this link.
In the case where the developer wants to implement the behavior described in 1., it can be usefull to use the strategy of exteding the SearchRecentSuggestionsProvider class. Details about how to do it can be reached in this link.
Implementing the search interface
This interface shall provide the following behavior:
When the user types a letter a call to the query method of the retrieved content provider class should return a filled cursor with the suggestion to be displayed in the list - you should take to not freeze the UI thread, so it I recommend to perform this search in an AsyncTask:
Inside the onPostExecute() method of your AsyncTask, you should retrieve a list (that should come from the doInBackground() method) containing the results to be displayed in the ResultList (you can map it in a POJO class and pass it to your custom adapter or you can use a CursorAdapter which would be the best practive for this task):
Identify when the user touches the search button from the soft keyboard. When he does that, send an intent to the searchable activity (the one responsible for handling the search result) and add the query as extra information in the intent
Identify when the user clicks in one of the displayed suggestions and send and intent containing the item information (this intent should be different from the one of the previous step)
The discribed steps should be enough for implementing an interface yourseld. All code examples were taken from here. I've made this library that does pretty much of what is described above. It is not well tested yet and some of the UI configuration might not be available yet.
I hope this answer might help someone in need.
I have set up a small tutorial to do that
http://drzon.net/how-to-create-a-clearable-autocomplete-dropdown-with-autocompletetextview/
Overview
I had to replace the
SearchView
withAutoCompleteTextView
as suggested.First, create an adapter. In my case it was a
JSONObject
ArrayAdapter. The data I wanted to display in the drop down was a venue name and venue address. Notice that the adapter must beFiltarable
and overridegetFilter()
Here is the custom
VenueFilter
:Now set up the layout for the search box (
actionbar_search.xml
) :And the layout for individual drop down item (venue name and venue address). This one looks bad, you'll have to customize it:
Next we want to put it in the action bar
That's about it, I still have some stuff to figure out :
SearchView
widget - a magnifier glass that opens up to a search box when you click it (and has a littleX
button to dismiss it and go back to normal)Overall this saves all the overhead of creating a searchable activity. Please add if you know how to customize it etc.
Please refer to this example which implements exactly what you requested: http://wptrafficanalyzer.in/blog/android-searchview-widget-with-actionbarcompat-library/
You need to use
collapseActionView
attribute.For example:
If you want to implement only drop down effect go for AutoCompleteTextView
And you can find a nice tutorial here
Then if you want to implement exact design you have to implement ActionBar and if you want to implement to lower version u need to implement ActionBarCombat instead of ActionBar
You should use ListPopupWindow and anchor it to the search view widget.