Our app's nearly finished. We just want to provide some sort of notification to the user if no photos match
their search query.
Now the class that provides the data to the recycler view for displaying, is our flickr recycler view
adapter class.
Now that's the easiest place to add a notification if there's nothing to display.
So let's open up that class.
Now the data's provided to the recycler view when it calls the onBindViewHolder function.
So all we have to do is check the list of photos and provide a suitably formatted holder
if there's nothing to display. Let's go ahead and do that, so on bind view holder.
So what we're going to do is add some code here at the start.
We're going to put if parentheses photoList.isEmpty closing parentheses and open a codeblock, and I'm
going to do a holder dot thumbnail dot setImageResource parentheses. And it's going to be R dot
drawerable.placeholder, and then we're going to put holder.title.text equals, in two double quotes, No photos
match your criteria,
or match your search is probably easier to understand.
Full stop backslash n backslash n Use the search icon to search for photos.
Then what we're going to do is change this at the end of the code block, code block and I'm going to put an else, and then another code block.
Then we're going to put the rest of the code up to, but not including, that final line of the holder dot title
dot text. We can put all this code in that code block. Paste it in there like so.
So basically that's now only going to be executed on the else.
So we're testing the photos first, and if it's null or contains no photos, we set the image to the placeholder
image and provide an informative message in the photo's
title text view. But if there is data we just execute the same code as before.
Now the set image resource function that I'm calling here on line 43, that loads the placeholder
image from the resources and puts it into the image view.
Now these backslash n characters, slash n slash n characters, they represent line breaks, and they're
a simple way to format text.
So Kotlin will replace the backslash n with a newline character when the text is displayed.
Now we've got this warning over here about a hardcoded string,
and we really should put that into a string resource.
And as we saw in a previous video, Android Studio does make that very easy for us.
So I'm just going to click on the text, like so, and do alt enter. Then I can come over here and use the lightbulb.
Now we saw that working earlier but at the moment it's not working here.
Now that's annoying but it's possibly not a bad thing.
Over-reliance on the code generators can lead to us not understanding the code, and it is useful to be
able to do things without using the code generator.
And yes I'm trying to be positive about this.
Sometimes things just don't work until Google releases a fix.
Now I may be trying to be positive about this problem, but don't think I'm making light of it.
When you're learning about all this, it can be very hard to work out if you're doing something wrong.
Most of the time you will be doing something wrong.
Bugs like this are actually quite rare, but when they do happen they can be very frustrating.
Now one thing that may help is to focus on what you're really trying to achieve.
Are we trying to get Android Studio to create a string resource for us, or do we just want to use a
string resource. So it would be nice if Android Studio did it for us,
but that's not our aim here.
Our aim is to remove those warnings and get the text into a resource.
So I'm not going to fight the tool here. We'll end up getting frustrated.
Instead what I'm going to do is cut the text out of the code, leaving the speech marks, like
so. Then I'm going to go to my strings.xml file, which is in our res folder and then the value
subfolder.
Double click on that.
We're going to add it right at the end here, and we're going to put, open a string tag, so string name equals,
and we'll call it empty underscore
photo, close the tag and I'm going to paste in the string, and you saw that pasting in text that contains
line breaks doesn't always work too well.
So if you get the text split over several lines as I have, then just remove those extra lines.
Basically just delete the line breaks and type in the slash in manually. So I'm going to go ahead and do that
and put in my two backslash n's there like so, and we're good to go there.
Now the next thing is to change the code to use the resource.
So go back to our flickr recycler view adapter and to our onBindViewHolder method.
So we want to change this code now to use the resource.
Now we can't do that by signing directly to the text property, but we can pass a resource ID to the set
text function, and that's easier than trying to get a resource object and extracting the string from
the resources. Now that worked fine in an activity, but isn't so easy in our adapter class. So I'm going to type, inside
the double quotes, holder dot
title. We can't use dot text, we use dot setText, parentheses R.string.empty underscore
photo.
So even without Android Studio's help that wasn't too hard.
There's really no excuse for hardcoding strings in your code.
That's the bottom line.
Alright so that should work, but the recycler view calls the getItemCount function to check if there's
any data to display, and it does that a lot.
So often in fact, that we commented out the logging in that function to keep the log cat a bit tidier.
This is the getItemCount, you can see that we've commented out the logging.
Now if there's no data, getItemCount will return 0,
and the recycler view won't attempt to display anything.
So we have to get our adapter to fib and tell he recycler view that there is at least one data item for it
to display.
Now the recycler view won't know or care that it's not a real flickr photo.
It'll just know that there's one item, and will go ahead and display it.
So let's actually add some code for that.
So on this line here,
all we're going to do is change the 0 to a 1.
So now if no photos match the tags that we search for, we get the placeholder image and a message.
Now that previous search for the Porsche driving monkeys should still be saved.
So let's run the app and see what happens.
So as you can see when I run the app, there still aren't any, and we're now getting this message and the placeholder
image.
So that's a better user experience than the blank screen we had before,
and it's actually well worth the extra effort to code it.
Alright so that's the end of this section.
So we've covered a lot of ground in this section.
We had to look at a different way to use async tasks to download data from the internet, then parse the json
data rather than the xml that we used in the top 10 downloader app.
Now the downloading and parsing involved using callback functions to notify the calling process that data
was available.
Now in the process,
we created our own interfaces for classes to implement, so that they could reliably provide callback
functions to the async classes.
And then we looked at using the recycler view, and recvcler view adapter classes, to display the data in
a scrolling list on the device.
And in order to view the photos at a larger size,
we had to use a gesture detector to work out what kind of input we were getting from the user.
And we responded to taps and long presses, while still allowing scroll events to be handled by the system.
We then had a good look at material design, before using it to style our flickr browser app,
along material design lines. Now in order to do that we had to learn about themes and styles,
and keeping a consistent look across different versions of Android.
And we finished off by looking at using string resources, rather than hardcoding text into our code
and layouts.
And finally we added a search feature to the app so that we could download different photos by specifying
different tags to select them.
So again that's the end of this section.
I'll see you in the next one.