1
00:00:05,620 --> 00:00:08,960
Alright, so moving on with our Searchable configuration.

2
00:00:09,000 --> 00:00:15,420
Now I've chosen to use the activity's label, rather than the app name in this Android label here.

3
00:00:15,570 --> 00:00:21,990
The documentation states that either can be used but that's a bit vague, and as we don't intend to use this

4
00:00:21,990 --> 00:00:24,050
app in the system's global search,

5
00:00:24,060 --> 00:00:26,300
it doesn't make sense to use the app name.

6
00:00:26,550 --> 00:00:32,520
And in fact Google have been discouraging the use of global search, and suggest using app indexing instead,

7
00:00:32,610 --> 00:00:37,100
so you can pretty much ignore any mention of global search now in the Android documentation.

8
00:00:37,490 --> 00:00:42,420
Alright so that's all we need for our Searchable Configuration file, and you'll see how the hint's used

9
00:00:42,750 --> 00:00:44,560
when we run the app and try searching.

10
00:00:44,820 --> 00:00:52,200
But next we need to make a couple of changes to the manifest file, so I'm going to open that up.

11
00:00:52,290 --> 00:00:57,690
Now the meta data that needs to be added can be lifted directly from the Create a Searchable Configuration

12
00:00:57,690 --> 00:01:01,140
section of the documentation that we talked about in the previous video.

13
00:01:01,350 --> 00:01:07,310
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,

14
00:01:08,280 --> 00:01:10,950
and the bit that we want here is this meta data line.

15
00:01:10,950 --> 00:01:15,570
So I'm going to copy that meta data line from the start and to the end.

16
00:01:15,570 --> 00:01:21,330
Copy that, basically linking that searchable xml file directly to the activity that's going to display

17
00:01:21,330 --> 00:01:22,230
the search view.

18
00:01:22,440 --> 00:01:25,280
So we'll go back to Android Studio,

19
00:01:25,590 --> 00:01:31,620
and we're looking at our search activity, and we basically want to put this meta data directly after the

20
00:01:31,620 --> 00:01:33,310
existing one in MainActivity.

21
00:01:33,360 --> 00:01:37,900
So I've already got that first bit of meta data there that ends in MainActivity,

22
00:01:37,950 --> 00:01:44,410
so after that we just want to paste in what we've copied from that web page, and put it in there as follows.

23
00:01:44,470 --> 00:01:50,490
And again, basically we're linking the searchable.xml file to the activity that's going to display

24
00:01:50,490 --> 00:01:51,220
the search view.

25
00:01:51,240 --> 00:01:56,630
In this case it's our SearchActivity. But the other thing we need to do, we also need to define an intent

26
00:01:56,630 --> 00:02:01,590
filter for the searchable activity which is search activity in our case.

27
00:02:01,620 --> 00:02:03,280
So let's go ahead and do that,

28
00:02:03,300 --> 00:02:07,690
and we'll put that above what we've just added, just after the MainActivity definition there. I'll make a bit of

29
00:02:07,750 --> 00:02:08,759
space to clear this out.

30
00:02:08,759 --> 00:02:17,560
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

31
00:02:17,560 --> 00:02:22,750
action, and it's going to be Android colon name equals two double quotes,

32
00:02:22,960 --> 00:02:25,830
and it's going to be Android, and notice up here we've got some options.

33
00:02:25,860 --> 00:02:27,810
We can actually select from the list, and I generally do

34
00:02:27,900 --> 00:02:29,200
recommend you do that.

35
00:02:29,410 --> 00:02:35,310
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

36
00:02:35,310 --> 00:02:40,020
the one that I want, which is the one there as you saw. And I'm going to close this one off,

37
00:02:41,850 --> 00:02:44,610
and I'll just make a bit of space so it's a bit easier to read.

38
00:02:44,610 --> 00:02:47,150
Now the main reason for this is one we don't need.

39
00:02:47,310 --> 00:02:53,430
When a search query is submitted, the search view will try to start the activity that has the android dot

40
00:02:53,460 --> 00:02:56,210
intent dot action dot search filter.

41
00:02:56,460 --> 00:03:02,070
Now the idea there is that the activity that's started, deals with the query. It performs the search using the

42
00:03:02,080 --> 00:03:03,780
details entered by the user.

43
00:03:04,020 --> 00:03:07,050
But in fact we're going to be doing things slightly differently.

44
00:03:07,050 --> 00:03:12,780
We're going to respond to a callback from the search view widget, and deal with the users query that way.

45
00:03:12,780 --> 00:03:20,120
Now unfortunately the getSearchable info function seems to need both the meta data and the intent filter,

46
00:03:20,280 --> 00:03:23,760
otherwise it doesn't identify the activity as searchable and we get an error.

47
00:03:23,760 --> 00:03:25,900
So that's the reason that we've put both those in there.

48
00:03:26,260 --> 00:03:29,570
Alright so at this point the manifest file has been updated,

49
00:03:29,790 --> 00:03:33,690
so the final step in getting search to work is to use a search manager.

50
00:03:33,690 --> 00:03:39,460
Now this retrieves the searchable configuration that we've just defined for search activity, then associates

51
00:03:39,510 --> 00:03:43,460
it with the search view widget that's imbedded in our toolbar.

52
00:03:43,470 --> 00:03:48,090
Now this is pretty much boilerplate code, and it's one of those times when we can just take advantage

53
00:03:48,330 --> 00:03:53,190
of the Android classes, without worrying too much about what they're doing in the background.

54
00:03:53,220 --> 00:03:54,900
Now I'm going to describe what we're doing,

55
00:03:54,990 --> 00:04:00,480
but again you don't need to fully understand all about the search manager and searchable info classes

56
00:04:00,840 --> 00:04:02,270
to be able to use them.

57
00:04:02,310 --> 00:04:09,480
Alright let's see the code that we need to add to SearchActivity's onCreateOptionsMenu, so we'll go back to Search

58
00:04:09,570 --> 00:04:13,850
Activity, and we need to put some code in our onCreateOptionsMenu as I said.

59
00:04:14,200 --> 00:04:18,850
So I'm going to put this after the menuInflater line and before the return to true.

60
00:04:19,160 --> 00:04:30,360
I'm going to put it there. So the code we want to add is val searchManager is equal to getSystemService parentheses,

61
00:04:30,900 --> 00:04:36,770
Context with a capital C dot, and it's going to be SEARCH_SERVICE closing parentheses,

62
00:04:37,110 --> 00:04:50,310
and as SearchManager. Then on the next line, searchView is equal to menu.findItem parentheses

63
00:04:50,690 --> 00:05:03,500
R.id dot app bar search, closing parentheses dot actionView as SearchView. Next line we're going to put

64
00:05:03,510 --> 00:05:15,280
val searchableInfo equals searchManager.getSearchableInfo parentheses component name. 

65
00:05:16,810 --> 00:05:18,530
Then on the next line searchView

66
00:05:18,980 --> 00:05:28,950
question mark dot setSearchableInfo parentheses searchableInfo closing parentheses, and then the last line

67
00:05:28,960 --> 00:05:30,520
we're going to put a searchView

68
00:05:31,280 --> 00:05:36,880
question mark dot isIconified is equal to true.

69
00:05:37,530 --> 00:05:43,850
Alright so the search manager provides access to the system search services, and the way to get a search manager

70
00:05:43,850 --> 00:05:46,850
instance is to call this getSystemService,

71
00:05:46,880 --> 00:05:53,330
and you can see me doing that on line 25. So basically, getSystemService is the correct way to get a search manager

72
00:05:53,330 --> 00:05:53,980
object.

73
00:05:54,290 --> 00:06:00,170
Now we then get a reference to the search view widget that's embedded in the search menu item on the

74
00:06:00,170 --> 00:06:06,140
toolbar, and because we're guaranteed to get a menu option, I'm going to actually remove the question mark there

75
00:06:06,200 --> 00:06:08,890
which makes line 26 valid.

76
00:06:09,320 --> 00:06:14,310
Alright so we've got our reference now to the search view widget that's embedded in the search menu item of the toolbar.

77
00:06:14,440 --> 00:06:20,870
We're doing that on line 26. So on line 27, we're getting the search manager to retrieve the searchable info

78
00:06:21,290 --> 00:06:26,640
from searchable.xml, by calling it's getSearchableInfo function.

79
00:06:26,720 --> 00:06:32,360
Now we need to provide getSearchableInfo with the component name of the activity that we want 

80
00:06:32,930 --> 00:06:34,410
the information for.

81
00:06:34,580 --> 00:06:40,460
So that's search activity here, and you could argue that it would make more sense to pass an activity rather

82
00:06:40,460 --> 00:06:43,620
than a component name, but it wants a component name

83
00:06:43,690 --> 00:06:44,990
so that's what we're going to give it.

84
00:06:45,470 --> 00:06:50,360
Now that searchable info is then set into the search view widget to configure it.

85
00:06:50,430 --> 00:06:51,950
Again you can see that on line 28.

86
00:06:52,190 --> 00:06:58,010
Now there's one other step we need to do before we're finished, and that's to set this isIconified

87
00:06:58,610 --> 00:07:01,630
on line 30, is to set that property essentially.

88
00:07:01,670 --> 00:07:06,940
Now we really want to pass false but it's easier to see what it does than to explain it.

89
00:07:07,080 --> 00:07:11,330
So I'm passing true here, and we'll change it once the app's working.

90
00:07:11,330 --> 00:07:14,410
Now we've seen a couple of the Google documents relating to searching.

91
00:07:14,420 --> 00:07:18,410
There's also a good guide which I'm going to put on the screen in a browser window.

92
00:07:21,550 --> 00:07:24,630
So this is a good one, and there's plenty of information available

93
00:07:24,700 --> 00:07:30,110
if you do want to understand what the search manager and searchable info classes do. And you can also google

94
00:07:30,110 --> 00:07:36,620
the class names to find the reference documentation of those individual classes. Now the search framework

95
00:07:36,700 --> 00:07:42,190
is incredibly powerful, and provides support for voice searching and suggestions while you type amongst other

96
00:07:42,190 --> 00:07:42,850
things.

97
00:07:43,000 --> 00:07:46,430
It doesn't do the searching though. All the steps we've just done

98
00:07:46,430 --> 00:07:53,100
are to get the search items from the user, using a consistent interface that they'll be familiar with.

99
00:07:53,680 --> 00:07:55,000
So let's go back to Android Studio.

100
00:07:55,000 --> 00:08:00,180
We're going to add some logging in the onCreateOptionsMenu function to show what's going on.

101
00:08:00,460 --> 00:08:04,900
So I'm going to start with some logging when the function starts, Log.d

102
00:08:05,920 --> 00:08:18,490
parentheses TAG comma double quotes, and it's going to be onCreateOptionsMenu colon starts, and we're getting into the habit

103
00:08:18,490 --> 00:08:20,450
of putting a, getting into the habit of putting a dot

104
00:08:20,450 --> 00:08:23,500
there so I'll do that again, or a period.

105
00:08:23,500 --> 00:08:29,980
Now let's also put some more code in and we'll do some after the searchView.setSearchable

106
00:08:29,980 --> 00:08:32,230
Info line.

107
00:08:32,770 --> 00:08:39,370
Let's put some logging in there. We want Log.d parentheses TAG comma dot onCreateOptionsMenu

108
00:08:42,530 --> 00:08:50,110
colon dollar componentName. Let's just take a copy of that because we're going to do this a couple more times,

109
00:08:50,210 --> 00:08:54,680
two more there. Next one's going to be hint,

110
00:08:54,820 --> 00:08:59,420
so let's do that, so we'll put hint is dollar,

111
00:08:59,670 --> 00:09:02,860
left and right curly braces, and put searchView

112
00:09:03,490 --> 00:09:08,180
question mark dot queeryHint. Next line

113
00:09:08,200 --> 00:09:16,380
we're going to print out the searchable info so dollar searchable info.

114
00:09:16,850 --> 00:09:20,910
Then lastly, let's take a copy of the first log,

115
00:09:21,700 --> 00:09:29,840
just before the return to true, we'll put return in there. Alright, so let's run

116
00:09:29,850 --> 00:09:31,650
this app now in an emulator,

117
00:09:31,890 --> 00:09:34,200
and let's have a look at what happens in our search code.

118
00:09:38,620 --> 00:09:43,720
Alright, I'm going to open log cat, and also open our app, 

119
00:09:46,660 --> 00:09:47,430
and once that starts

120
00:09:47,470 --> 00:09:55,920
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

121
00:09:55,940 --> 00:10:03,430
onCreate function was called, Searchactivity onCreate starts. And then you can see the various logging

122
00:10:03,430 --> 00:10:07,720
that we added to our onCreateOptionsMenu, and scrolling down

123
00:10:07,720 --> 00:10:14,380
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

124
00:10:14,380 --> 00:10:18,920
dot xml file. So it's basically retrieved that as you can see, it says "Enter photo tags".

125
00:10:19,180 --> 00:10:21,570
Now going back to the display.

126
00:10:21,610 --> 00:10:22,970
There's not much to see there,

127
00:10:23,200 --> 00:10:25,930
just a back button and another search icon to click.

128
00:10:25,930 --> 00:10:31,180
Now we really wanted the search widget to be displayed in the toolbar, which is what happens now if I

129
00:10:31,180 --> 00:10:36,350
click the, or tap rather, the search icon on the screen.

130
00:10:36,370 --> 00:10:39,980
So this is how the screen should look when the activity launches.

131
00:10:40,100 --> 00:10:45,010
And we can actually force the search view to be opened like this, instead of being displayed as

132
00:10:45,010 --> 00:10:49,460
an icon by setting the isIconified to false instead of true.

133
00:10:49,720 --> 00:10:56,070
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

134
00:10:56,080 --> 00:10:58,710
now just to see what it looks like again.

135
00:11:06,210 --> 00:11:07,710
OK I'm going to come over here now,

136
00:11:08,120 --> 00:11:12,870
and this time when I click on the search box, or the search icon rather, the search widget's ready for us

137
00:11:12,870 --> 00:11:14,310
to type our search into,

138
00:11:14,460 --> 00:11:17,230
without me having to click that icon a second time.

139
00:11:17,280 --> 00:11:19,680
So that's what isIconified property does.

140
00:11:19,830 --> 00:11:24,690
It sets whether the widget should appear as an icon, or open and be ready for business, which is what

141
00:11:24,690 --> 00:11:25,910
we want here.

142
00:11:26,070 --> 00:11:30,220
By the way you can also see the hint text appearing now in the search box.

143
00:11:30,370 --> 00:11:35,180
Now although we didn't put a lot of customization in our searchable.xml configuration file,

144
00:11:35,520 --> 00:11:41,980
it has made the app a bit friendlier, by giving the user a hint that they can search for photo tags.

145
00:11:41,980 --> 00:11:47,280
Now if you experiment with the app there's actually a problem when you tap. So if we enter some text,

146
00:11:50,340 --> 00:11:51,310
pressing enter there,

147
00:11:51,480 --> 00:11:56,120
and you can see what happened, that the search activity immediately starts up again.

148
00:11:56,130 --> 00:11:57,620
Now that's actually normal,

149
00:11:57,720 --> 00:11:59,980
and we will be fixing that behavior in our code,

150
00:12:00,150 --> 00:12:04,200
but for now I'm going to stop the video here. In the next one we're going to write the code to actually

151
00:12:04,200 --> 00:12:05,710
perform the searches.

152
00:12:05,940 --> 00:12:06,980
So I'll see you in the next video.

