Alright, so moving on with our Searchable configuration.
Now I've chosen to use the activity's label, rather than the app name in this Android label here.
The documentation states that either can be used but that's a bit vague, and as we don't intend to use this
app in the system's global search,
it doesn't make sense to use the app name.
And in fact Google have been discouraging the use of global search, and suggest using app indexing instead,
so you can pretty much ignore any mention of global search now in the Android documentation.
Alright so that's all we need for our Searchable Configuration file, and you'll see how the hint's used
when we run the app and try searching.
But next we need to make a couple of changes to the manifest file, so I'm going to open that up.
Now the meta data that needs to be added can be lifted directly from the Create a Searchable Configuration
section of the documentation that we talked about in the previous video.
Let's just go back to that again, and I'll just click on back here, to go to that page that had the documentation,
and the bit that we want here is this meta data line.
So I'm going to copy that meta data line from the start and to the end.
Copy that, basically linking that searchable xml file directly to the activity that's going to display
the search view.
So we'll go back to Android Studio,
and we're looking at our search activity, and we basically want to put this meta data directly after the
existing one in MainActivity.
So I've already got that first bit of meta data there that ends in MainActivity,
so after that we just want to paste in what we've copied from that web page, and put it in there as follows.
And again, basically we're linking the searchable.xml file to the activity that's going to display
the search view.
In this case it's our SearchActivity. But the other thing we need to do, we also need to define an intent
filter for the searchable activity which is search activity in our case.
So let's go ahead and do that,
and we'll put that above what we've just added, just after the MainActivity definition there. I'll make a bit of
space to clear this out.
So it's going to be intent filter, and close that off, then in the middle there we're going to put, start a tag
action, and it's going to be Android colon name equals two double quotes,
and it's going to be Android, and notice up here we've got some options.
We can actually select from the list, and I generally do
recommend you do that.
The one we want is android dot intent dot action dot search, so I can do a bit more typing or I can just scroll down and find
the one that I want, which is the one there as you saw. And I'm going to close this one off,
and I'll just make a bit of space so it's a bit easier to read.
Now the main reason for this is one we don't need.
When a search query is submitted, the search view will try to start the activity that has the android dot
intent dot action dot search filter.
Now the idea there is that the activity that's started, deals with the query. It performs the search using the
details entered by the user.
But in fact we're going to be doing things slightly differently.
We're going to respond to a callback from the search view widget, and deal with the users query that way.
Now unfortunately the getSearchable info function seems to need both the meta data and the intent filter,
otherwise it doesn't identify the activity as searchable and we get an error.
So that's the reason that we've put both those in there.
Alright so at this point the manifest file has been updated,
so the final step in getting search to work is to use a search manager.
Now this retrieves the searchable configuration that we've just defined for search activity, then associates
it with the search view widget that's imbedded in our toolbar.
Now this is pretty much boilerplate code, and it's one of those times when we can just take advantage
of the Android classes, without worrying too much about what they're doing in the background.
Now I'm going to describe what we're doing,
but again you don't need to fully understand all about the search manager and searchable info classes
to be able to use them.
Alright let's see the code that we need to add to SearchActivity's onCreateOptionsMenu, so we'll go back to Search
Activity, and we need to put some code in our onCreateOptionsMenu as I said.
So I'm going to put this after the menuInflater line and before the return to true.
I'm going to put it there. So the code we want to add is val searchManager is equal to getSystemService parentheses,
Context with a capital C dot, and it's going to be SEARCH_SERVICE closing parentheses,
and as SearchManager. Then on the next line, searchView is equal to menu.findItem parentheses
R.id dot app bar search, closing parentheses dot actionView as SearchView. Next line we're going to put
val searchableInfo equals searchManager.getSearchableInfo parentheses component name.
Then on the next line searchView
question mark dot setSearchableInfo parentheses searchableInfo closing parentheses, and then the last line
we're going to put a searchView
question mark dot isIconified is equal to true.
Alright so the search manager provides access to the system search services, and the way to get a search manager
instance is to call this getSystemService,
and you can see me doing that on line 25. So basically, getSystemService is the correct way to get a search manager
object.
Now we then get a reference to the search view widget that's embedded in the search menu item on the
toolbar, and because we're guaranteed to get a menu option, I'm going to actually remove the question mark there
which makes line 26 valid.
Alright so we've got our reference now to the search view widget that's embedded in the search menu item of the toolbar.
We're doing that on line 26. So on line 27, we're getting the search manager to retrieve the searchable info
from searchable.xml, by calling it's getSearchableInfo function.
Now we need to provide getSearchableInfo with the component name of the activity that we want
the information for.
So that's search activity here, and you could argue that it would make more sense to pass an activity rather
than a component name, but it wants a component name
so that's what we're going to give it.
Now that searchable info is then set into the search view widget to configure it.
Again you can see that on line 28.
Now there's one other step we need to do before we're finished, and that's to set this isIconified
on line 30, is to set that property essentially.
Now we really want to pass false but it's easier to see what it does than to explain it.
So I'm passing true here, and we'll change it once the app's working.
Now we've seen a couple of the Google documents relating to searching.
There's also a good guide which I'm going to put on the screen in a browser window.
So this is a good one, and there's plenty of information available
if you do want to understand what the search manager and searchable info classes do. And you can also google
the class names to find the reference documentation of those individual classes. Now the search framework
is incredibly powerful, and provides support for voice searching and suggestions while you type amongst other
things.
It doesn't do the searching though. All the steps we've just done
are to get the search items from the user, using a consistent interface that they'll be familiar with.
So let's go back to Android Studio.
We're going to add some logging in the onCreateOptionsMenu function to show what's going on.
So I'm going to start with some logging when the function starts, Log.d
parentheses TAG comma double quotes, and it's going to be onCreateOptionsMenu colon starts, and we're getting into the habit
of putting a, getting into the habit of putting a dot
there so I'll do that again, or a period.
Now let's also put some more code in and we'll do some after the searchView.setSearchable
Info line.
Let's put some logging in there. We want Log.d parentheses TAG comma dot onCreateOptionsMenu
colon dollar componentName. Let's just take a copy of that because we're going to do this a couple more times,
two more there. Next one's going to be hint,
so let's do that, so we'll put hint is dollar,
left and right curly braces, and put searchView
question mark dot queeryHint. Next line
we're going to print out the searchable info so dollar searchable info.
Then lastly, let's take a copy of the first log,
just before the return to true, we'll put return in there. Alright, so let's run
this app now in an emulator,
and let's have a look at what happens in our search code.
Alright, I'm going to open log cat, and also open our app,
and once that starts
I'm going to come over here and click on the search icon. And if we have a look at the actual code, we can see the search activity's
onCreate function was called, Searchactivity onCreate starts. And then you can see the various logging
that we added to our onCreateOptionsMenu, and scrolling down
we can now see that the search view's set up properly now, because we've logged the hint over here, and it came back from the searchable
dot xml file. So it's basically retrieved that as you can see, it says "Enter photo tags".
Now going back to the display.
There's not much to see there,
just a back button and another search icon to click.
Now we really wanted the search widget to be displayed in the toolbar, which is what happens now if I
click the, or tap rather, the search icon on the screen.
So this is how the screen should look when the activity launches.
And we can actually force the search view to be opened like this, instead of being displayed as
an icon by setting the isIconified to false instead of true.
So let's go back to our code and do that, setting this down here, this time to false, and we'll run it again
now just to see what it looks like again.
OK I'm going to come over here now,
and this time when I click on the search box, or the search icon rather, the search widget's ready for us
to type our search into,
without me having to click that icon a second time.
So that's what isIconified property does.
It sets whether the widget should appear as an icon, or open and be ready for business, which is what
we want here.
By the way you can also see the hint text appearing now in the search box.
Now although we didn't put a lot of customization in our searchable.xml configuration file,
it has made the app a bit friendlier, by giving the user a hint that they can search for photo tags.
Now if you experiment with the app there's actually a problem when you tap. So if we enter some text,
pressing enter there,
and you can see what happened, that the search activity immediately starts up again.
Now that's actually normal,
and we will be fixing that behavior in our code,
but for now I'm going to stop the video here. In the next one we're going to write the code to actually
perform the searches.
So I'll see you in the next video.