IAN LAKE: For many, how you use and navigate
your app can be classified in one of two ways,
either browsing through content, or by directed search
for specific content.
If your app isn't delivering a good in-app search experience,
you're only serving part of your user base.
Hi, I'm Ian Lake, and I want to show you
what goes into building a great in-app search experience using
SearchView, to help your users get to exactly what they
want as easily as possible.
The basic building blocks of a good search experience
start with perhaps the hardest one, good content.
We're talking about searching using a single query string.
But how that maps onto your data--
be it a list of users or a full text search of posts--
and how you store that data is all in your hands.
Well, it may be helpful to have a special Full Text
Search, or FTS, Table locally on the device,
if your data set is small enough and query speed is critical.
For now, let's focus on the search process itself.
With search being such a core navigation pattern,
it makes sense to standardize on a single pattern
for providing search.
For Android, that involves an aptly-named SearchView.
Usually, the SearchView is seen as an action in the main app
bar, or an appropriate toolbar.
By tapping on the SearchView icon,
the SearchView expands inline, allowing users
to immediately search.
Through AppCompat, part of the Android Support Library,
SearchView is available on all Android 2.1 and higher devices.
Adding a SearchView to your app bar
involves just adding another item to the menu you inflate.
Besides the more obvious title and icon,
you'll note the showAsAction contains collapseActionView.
This is what tells the menu that this item should be collapsed
by default, but can be expanded into the full actionViewClass
that's defined.
We also need ifRoom-- or alternatively, always--
for showAsAction.
Without this, our icon will only appear in the overflow menu
and we won't be able to expand it into the full SearchView.
Not what we want.
With just this addition, we get a nice search icon
that expands into the SearchView,
but the search doesn't really go anywhere or do anything.
We can get a reference to it when we inflate our menu,
but where do we go from there?
Firstly, there's full manual mode,
using setOnQueryTextListener.
Here, you'll get a callback for when the search text changes
and when the user submits their search, giving you
full control over exactly what happens when.
You can take advantage of a lot more when you instead provide
a searchable configuration.
This is an XML file, specifically
for all the various options relating to search.
So many options that we have a full documentation page just
on all the various options, all nine pages of the detailed
explanations.
Thankfully, it can also be super short and easy.
Just a label-- which would usually
be your app name-- and a hint in the form of search posts
or search your app name.
Guess what it takes to add voice search to your SearchView?
Just a single additional line to your searchable configuration.
That's a pretty powerful line, but remember that voice search
directly submits what is said.
There's no opportunity to change things before submitting,
so make sure your searches lend themselves
to voice before enabling it.
Then you'll associate that searchable configuration
with an activity.
This is the activity you want to go to when
the user submits a query.
You'll note we use singleTop launch mode.
This is important if you have a SearchView
on your searchable activity-- say, to do a second search.
And this launch mode prevents a potential giant back
stack of search result screens.
Hooking up your SearchView to a searchable configuration
uses SearchManager, the system service
which parses that metadata element you've
added to one of your activities and creates
a searchable info you can pass to your SearchView.
This launches our searchable activity.
But to extract the actual search text, we look for action search
and then extract the query from the intent's extra.
You call this from onCreate, and if you
are using that singleTop launch mode,
call it from onNewIntent, as well.
So you have a great-looking SearchView
that directly searches to the activity of our choice.
What we haven't talked about yet are recent query suggestions,
or custom suggestions, which make
things even easier for users to find what they want.
Check out the developer site for more information.
I can't wait to see what awesome experiences
you come up with using in-app search on your quest
to build better apps.
[MUSIC PLAYING]