1 00:00:05,280 --> 00:00:09,820 So I finished the last video by noting that our app does have one serious shortcoming. 2 00:00:09,850 --> 00:00:14,010 It can only display photos that are tagged with Android and Oreo. 3 00:00:14,260 --> 00:00:19,060 Now the last bit of functionality we need to provide is a search feature. Now we've already created 4 00:00:19,060 --> 00:00:20,420 the search activity, 5 00:00:20,470 --> 00:00:25,180 so in the next couple of videos we're going to have a look at the Android search view, and see how we 6 00:00:25,180 --> 00:00:28,630 can use that to allow users to search for different photos. 7 00:00:28,820 --> 00:00:33,180 Now the first step though, is to change the main menu to include a search option. 8 00:00:33,430 --> 00:00:35,130 So we want to open our main menu. 9 00:00:35,140 --> 00:00:39,200 We do that by going into our res folder, and going into the menu folder, 10 00:00:39,340 --> 00:00:44,660 and we want to double click our menu underscore main dot xml to edit it in the layout designer. 11 00:00:44,710 --> 00:00:49,330 Now at the moment, the menu only has a single item over here for settings as you can see, and you can see the 12 00:00:49,330 --> 00:00:51,140 option also in the component tree. 13 00:00:51,250 --> 00:00:52,950 So we're not actually going to be using settings, 14 00:00:52,960 --> 00:00:54,130 so we're going to delete that. 15 00:00:54,280 --> 00:00:58,080 I can delete that on the component tree. 16 00:00:58,180 --> 00:01:04,620 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. 17 00:01:06,050 --> 00:01:13,190 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 18 00:01:13,190 --> 00:01:14,170 item was chosen. 19 00:01:14,360 --> 00:01:20,500 So let's start by calling this one action underscore search. 20 00:01:20,510 --> 00:01:22,400 Now the title's going to be search. 21 00:01:22,510 --> 00:01:27,380 I'm going to create a string resource for this and we'll call it activity underscore search. 22 00:01:27,380 --> 00:01:33,700 So I'm going to delete the existing text using the ellipsis, Add new resource, New String Value, 23 00:01:33,900 --> 00:01:41,910 and again we're going to call this one action underscore search, and the Resource value, Search, and we're 24 00:01:41,910 --> 00:01:45,060 storing that in strings.xml again. Press enter there. 25 00:01:45,390 --> 00:01:50,580 So basically that text will be displayed when the menu's expanded, but it's better to show an icon on 26 00:01:50,580 --> 00:01:55,590 the toolbar. Now to choose an icon, I'm going to click on the ellipsis next to the icon property, 27 00:01:55,610 --> 00:01:59,730 over here, and that brings up the resources dialogue. 28 00:01:59,920 --> 00:02:04,870 Then you want to make sure that Drawable's selected over here to the left, which it is. So to find the default 29 00:02:04,900 --> 00:02:12,250 Android search icons, just come up here and type in here, search. Now when it finds them all, 30 00:02:12,250 --> 00:02:13,860 we use the first one in the list here, 31 00:02:13,960 --> 00:02:14,780 ic_menu_search. 32 00:02:14,780 --> 00:02:19,720 Click on that and it gives you a graphical representation of what that looks 33 00:02:19,720 --> 00:02:20,160 like, the icon, 34 00:02:20,160 --> 00:02:22,160 and click on OK. 35 00:02:22,750 --> 00:02:26,290 The next thing we need to do is set the showAsAction property, 36 00:02:26,290 --> 00:02:33,610 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. 37 00:02:33,680 --> 00:02:36,440 Now always may sound like the one we want, 38 00:02:36,610 --> 00:02:41,020 but room is the preferred option, when you want to show the icon in the toolbar. 39 00:02:41,060 --> 00:02:46,360 So always will try to always show the item, even if that would result in it overlapping other menu 40 00:02:46,360 --> 00:02:47,380 items. 41 00:02:47,380 --> 00:02:51,170 Now there is actually a full description of these settings on this page. 42 00:02:51,190 --> 00:02:55,240 Let's just go down and quickly we'll go to that page and quickly have a look at it. 43 00:02:55,330 --> 00:02:57,670 It's worth actually knowing this. 44 00:02:58,010 --> 00:02:59,320 I'll paste that in, 45 00:03:02,400 --> 00:03:09,390 and scroll down to about the one third mark, and there's a good description for our showAsAction. There it 46 00:03:09,390 --> 00:03:10,620 is on the screen there. 47 00:03:11,190 --> 00:03:16,800 So we've only got one item on our menu, but it's worth becoming familiar with these options for when 48 00:03:16,800 --> 00:03:19,190 you start creating more complex menus. 49 00:03:19,200 --> 00:03:25,680 So based on these options here, I'm going to tick ifRoom because that seems to be the best option there, 50 00:03:26,120 --> 00:03:28,840 "Only place this item in the app bar if there's room for it". 51 00:03:29,130 --> 00:03:32,340 Let's go back to Android Studio. I'm going to open that 52 00:03:32,570 --> 00:03:34,540 again by clicking the ellipsis, 53 00:03:34,600 --> 00:03:38,320 click on ifRoom, and I'm going to click on OK. 54 00:03:38,330 --> 00:03:43,600 Now I'm going to switch to text briefly, down here, by clicking on the text tab, because there's something 55 00:03:43,600 --> 00:03:45,580 I want to mention in the xml. 56 00:03:45,720 --> 00:03:51,120 Now most of the attributes for the menu item are in the Android namespace. 57 00:03:51,220 --> 00:03:56,350 So they're a part of the Android framework and if you search the documentation, you find that they were available 58 00:03:56,350 --> 00:04:05,320 from API one. We'll go back to the documentation. So notice that showAsAction was actually, is actually introduced, 59 00:04:05,320 --> 00:04:12,280 or showing as introduced in API 11. But scrolling up a bit, things like the title, the icon, 60 00:04:13,000 --> 00:04:17,529 they don't mentioned an API level, so in other words they were there from the beginning. The documentation doesn't 61 00:04:17,529 --> 00:04:23,650 always specify the level with the API levels 1. So again, the Android showAsAction was introduced as 62 00:04:23,740 --> 00:04:25,580 API level 11, 63 00:04:25,750 --> 00:04:31,540 and to make it available on older devices, the property's also been added to the support libraries. 64 00:04:31,540 --> 00:04:33,980 Now remember that we're using the support toolbar, 65 00:04:34,090 --> 00:04:40,150 so the property that we want is in our app's namespace, not in the Android namespace. And going back to 66 00:04:40,540 --> 00:04:47,480 Android Studio again, that's why showAsAction is prefixed with app colon, rather than Android colon. 67 00:04:47,770 --> 00:04:51,920 Alright so that's how menu finished. Clicking it obviously won't do anything yet. 68 00:04:52,210 --> 00:04:56,230 What we want to do is launch the search activity, and we'll do that in the next video.