So I finished the last video by noting that our app does have one serious shortcoming.
It can only display photos that are tagged with Android and Oreo.
Now the last bit of functionality we need to provide is a search feature. Now we've already created
the search activity,
so in the next couple of videos we're going to have a look at the Android search view, and see how we
can use that to allow users to search for different photos.
Now the first step though, is to change the main menu to include a search option.
So we want to open our main menu.
We do that by going into our res folder, and going into the menu folder,
and we want to double click our menu underscore main dot xml to edit it in the layout designer.
Now at the moment, the menu only has a single item over here for settings as you can see, and you can see the
option also in the component tree.
So we're not actually going to be using settings,
so we're going to delete that.
I can delete that on the component tree.
So what I want to do now is add another menu item by dragging it from the palette. So I'm going to add it to the component tree.
I'll expand it so we can see it. OK I'm going to select it, and we need an ID so that we can tell which menu
item was chosen.
So let's start by calling this one action underscore search.
Now the title's going to be search.
I'm going to create a string resource for this and we'll call it activity underscore search.
So I'm going to delete the existing text using the ellipsis, Add new resource, New String Value,
and again we're going to call this one action underscore search, and the Resource value, Search, and we're
storing that in strings.xml again. Press enter there.
So basically that text will be displayed when the menu's expanded, but it's better to show an icon on
the toolbar. Now to choose an icon, I'm going to click on the ellipsis next to the icon property,
over here, and that brings up the resources dialogue.
Then you want to make sure that Drawable's selected over here to the left, which it is. So to find the default
Android search icons, just come up here and type in here, search. Now when it finds them all,
we use the first one in the list here,
ic_menu_search.
Click on that and it gives you a graphical representation of what that looks
like, the icon,
and click on OK.
The next thing we need to do is set the showAsAction property,
this is this one over here. Now, I'm going to come over here and get the list of options by clicking on the ellipsis.
Now always may sound like the one we want,
but room is the preferred option, when you want to show the icon in the toolbar.
So always will try to always show the item, even if that would result in it overlapping other menu
items.
Now there is actually a full description of these settings on this page.
Let's just go down and quickly we'll go to that page and quickly have a look at it.
It's worth actually knowing this.
I'll paste that in,
and scroll down to about the one third mark, and there's a good description for our showAsAction. There it
is on the screen there.
So we've only got one item on our menu, but it's worth becoming familiar with these options for when
you start creating more complex menus.
So based on these options here, I'm going to tick ifRoom because that seems to be the best option there,
"Only place this item in the app bar if there's room for it".
Let's go back to Android Studio. I'm going to open that
again by clicking the ellipsis,
click on ifRoom, and I'm going to click on OK.
Now I'm going to switch to text briefly, down here, by clicking on the text tab, because there's something
I want to mention in the xml.
Now most of the attributes for the menu item are in the Android namespace.
So they're a part of the Android framework and if you search the documentation, you find that they were available
from API one. We'll go back to the documentation. So notice that showAsAction was actually, is actually introduced,
or showing as introduced in API 11. But scrolling up a bit, things like the title, the icon,
they don't mentioned an API level, so in other words they were there from the beginning. The documentation doesn't
always specify the level with the API levels 1. So again, the Android showAsAction was introduced as
API level 11,
and to make it available on older devices, the property's also been added to the support libraries.
Now remember that we're using the support toolbar,
so the property that we want is in our app's namespace, not in the Android namespace. And going back to
Android Studio again, that's why showAsAction is prefixed with app colon, rather than Android colon.
Alright so that's how menu finished. Clicking it obviously won't do anything yet.
What we want to do is launch the search activity, and we'll do that in the next video.