In the last video, we added the search menu item. Now clicking it won't do anything yet. The main reason for that is
we've got an error now in MainActivity, in the onOptionsItemSelected, because we've removed the settings
menu option.
So it's time to get that working now.
So what we want to do is launch the searchActivity activity. Now we've seen how to start a new activity
using an intent, a few times now.
So this is going to be very similar to starting our PhotDetailsActivity when a thumbnail's tapped.
So the place to do it, again, is in this onOptionsItemSelected in MainActivity, and the change,
firstly, we're going to remove the action underscore settings because we deleted that menu option.
We're going to replace that with action
underscore search, which is our new one, delete true, add a code block, and the code block is going to be start
Activity parentheses Intent with a capital I,
then parentheses again, this comma.
Then it's going to be searchActivity colon colon class dot Java.
That's the first one,
and the second line is going to be true.
So although we haven't done very much, and it's just basically launching of an activity like we've done
before,
it's still a good idea to run the app before going any further.
So let's actually try doing that, making sure that the app compiles and runs.
OK, so
there's our app, and we've now got the little magnifying glass here, the search icon
so we click on that, we can see that searchActivity actually works. But of course, it's not going to do anything
yet.
But if we know that it launches OK, then any errors we get later will be caused by changes in search
Activity,
so that narrows down where we have to look for them.
So that's why it's always a good idea to be making, or testing your changes as you go along.
Alright, so we've got the search icon working, so that's good.
So it's now time to make our searchActivity do something useful.
Now it turns out there's actually not much we need to do, because the search view widget does most of it for
us. Now we need to create a new menu,
so I'm going to come over here to our res folder. I'm going to open that up, the res Menu folder,
right click, select New, Menu resource file.
I'm going to call this one menu_search and click OK.
We don't change anything else.
Now it's just going to contain a single item which would be where we enter the search terms.
So what we're going to do here, is use the toolbar to host a search view widget, so that we can search
for things by typing them into the toolbar.
And that's a very common way to implement search in Android.
So it'll pretty much look as you'd expect it to.
So I'm going to come over here and grab, and drag rather, a search item onto the menu, and obviously search
item rather than menu item, so we're going to just do that and release, and obviously I could've dragged it into the component tree,
if I wanted to do that instead.
Alright so that's created the search view for us, you can see over here that as well. The ID has been set to
app underscore bar underscore search.
So I'm just going to leave it at that.
Now I have noticed that the common attributes pane doesn't always populate, and you can see there that
I've clicked on that and nothing's happening.
If that appears blank for you, just expand the attributes pane and that should show all the attributes.
So I'll come over here and do that, and you can see that that's actually working now.
And if it comes up with a different ID for you, either change it to be the same as mine, or just use the
ID you've got when you type the code in later. And again, if I go back we can't see anything else, but you can see by
the extended view we can see that it's working.
Now we've got an error though, or I have and you may not have.
If we come over here, and I've already expanded menu but you can see over here, we've got a warning or an error over
here.
And if you click on this exclamation mark,
the error's saying that we should be using the app colon namespace with the appCompat library, and it's actually
a slightly strange error.
So if we scroll up a little bit more,
it's actually got a button here, a fix, that'll change to use the app colon namespace.
Now if Android Studio can spot the error and fix it for us, then it may fix it automatically in future.
So if you don't get this particular error that's fine. There's no point worrying when you don't get an error.
Save your worrying for when you do get them.
Alright so I'm going to click on this fix button, and you can see that we've actually got a message
here about hardcoded text, but the other message has disappeared,
so that's good.
Now what we should probably do is make a point of clearing or fixing this hardcoded text warning
as well. So I'm going to close this for now,
and as I showed you earlier,
for my version of Android Studio the common attributes aren't showing, and so therefore I'm having to click
on the extended attributes to get in there and be able to change anything.
And what we want to do is to make some changes here.
So we've already got our string resource for search,
so we're going to go over next to title, over here, and click on the ellipsis. Then we want to choose activity
underscore search, so I've got title underscore activity underscore search.
Click on OK,
and you can see that it's now cleared the error. Now the last attribute is set to, to set is showAsAction.
So we want to set that to
ifRoom, so this one down here, showAsAction. We're going to come over here, expand this out, and click on ifRoom. Alright so at this
point we've got our menu created.
Now we need to tell search activity to display it and provide the search functionality that we need.
So I'm going to go through the easy stuff first,
and then we can have a look at how search is implemented, and the changes we have to make to get it to
work.
So the first thing we need though, is to declare a search view variable in search activity. So I'm going to come over and
open that up.
SearchActivity.
Let's go back under the tag definition and add a search view, so it's going to be private var
search view colon SearchView
question mark
equals null, and we're going to select,
now you might be asking which one to select here.
Well we're using the framework search view rather than the support version,
and that's because SearchView was added in API 11.
Now our minimum API is 17,
so therefore we don't need to use the support library for this. Now as Android Studio
doesn't now support API levels earlier than 14,
it's even strange that it's offering the support library version. It's something to be aware of though and watch out
for.
So I'm going to select the main version android.widget.SearchView. Now the next step is to inflate
menu in the onCreateOptions menu function, which we'll add after the on create function.
So I'm going to come down here, then I'm going to do a control o to generate the stub, and again, the one we want we want is
onCreateOptions. I'm just typing a bit of that in, that one there, onCreateOptionsMenu is the one we want so I'm just going
to press enter there.
And we're going to delete the call to super,
and instead what we're going to do is type menu inflator dot inflate parentheses R.menu dot
menu underscore search, which we created,
comma menu closing parentheses.
And then we're going to return true.
So that's standard code in the onCreateOptionsMenu function, and it's the same as we did in MainActivity,
except of course this time we're inflating menu underscore search instead of menu underscore main. Now
inflating an XML layout, just takes the XML representation of all the widgets and menu items and
so forth, and creates the view for them, or from them.
Now here we're using a menu inflater rather than a layout inflater, because we're actually creating
a menu, or inflating a menu, rather than a layout.
Alright so that's the easy bits out of the way.
Now it gets interesting.
Now the steps we have to do at this point are well documented, and a good source is a Google training document
that I'm going to open up in my browser now, and add as a resource as I always do,
so you can check it out at your leisure. Now this particular page doesn't mention that you have to use
the app colon namespace,
if you're using AppCompat, but the rest of the document is accurate. Now we've actually done the first
steps on this page.
We've added the search view to the app bar,
so what we need now to do is create a Searchable Configuration, and that's all described on that heading.
So if we scroll down here, create a Searchable configuration,
and we're going to be coming back to this page to get the meta data for the manifests.
Now we're going to create a very basic configuration file, but if we follow the Searchable Configuration
link at the top of that section of the page, there's a lot of configuration possibilities, so Searchable
Configuration here.
So you can see there's quite a few options that we can define. Now we're going to provide a label because that's
required,
and we can come down to any of these and see whether they're required or not.
You can see here String resource required, and we're also going to provide a hint, which if we scroll down and have
a look there, the hint
that's actually recommended.
Everything else is optional and lets you provide lots of things including, for example, voice search.
Now we're going to keep things simple for this first example of using search in our apps.
So our Searchable Configuration file will only contain the label and the hint.
Now the Searchable Configuration file is usually called searchable dot xml, although frankly
you can call it any valid name you want, which you may need to do if your app implements several different
search features.
Now the file that you create must live in the res slash xml directory,
so therefore we need to create that xml directory.
So going back to Android Studio,
we'll go into the res folder,
and from the res folder we're going to create a new folder. We're going to right click New, Android resource directory. So
we're going to change it so that the name will change from values to xml, and the Resource type, we'll also change that
so that's also xml. Everything else is fine to leave as the default
so just click on OK.
So at this point we've now got an xml folder down here. So we want to right click on that folder now, and click
on New, XML resource file.
Now we don't need to specify anything other than the file name,
so we can just type in searchable here.
Click OK,
and the xml extension will be provided automatically by Android Studio, and we can confirm that by
opening up the directory there and you can see that showing up.
The other thing that is different though, is that Android Studio's automatically opened up searchable dot
xml in the new preferences editor.
Now this was new to Android Studio 2.2, and it lets you create a settings screen for your app settings.
Now that's great if you're trying to create a settings screen, but less useful if you're creating what
we're doing, a searchable configuration file.
Now there is a way to prevent it from launching straight into this preferences editor
when you create a new xml file, but frankly it's not really worth the trouble, because all you really
have to do is come over here and click on text to get into a normal editor, where you can edit the xml
directly. Now notice that
it also creates the xml declaration for us.
That's that first line, line 1, and that saves a little bit of typing.
So what I'm going to do is replace the preferences screen tags with the searchable tag that we need.
So literally I'm just going to come up here and change that to searchable, press enter there. I'm going to come over to the
end of this first line for Android.
I'm going to add to that, android colon hint equals Enter photo tags, and on the next line, tab over so that
it's nicely in line, so android label equals. Then we're going to print double quotes @string forward slash
title_ activity_search,
and I'm just going to close it off there, and just delete the closing tag there now that we've done that. Now for the hint,
I going to show you an alternate way of doing things.
As I've actually entered the actual text that I want, what you can do with your cursor in that field,
is you can hold down alt and press enter,
and it brings up a little menu here. We can select Extract string resource, and that actually lets you create, or convert
rather, the text to a string resource.
Now I'm going to use the name searchable_hint for the resource name.
Now note that the resource value is already set to whatever text you typed in the double quotes there. So if you just
click on OK now, you can see that the text is now stored in a string resource instead of being hardcoded.
Alright so let's finish the video here.
We'll continue on working on the changes to implement search in the next video.