Welcome back everyone. Our Settings Dialog's coming along well. We've only
got three more things to implement, before it's finished. In this video, we'll
get it to display the values that the user selects. I'll run the app again and
go into settings, so we can see what we're about to change. The text above the
seek bar doesn't look good. It's showing % 1d space % 2s at the end. What we want
to say is something like, ignore timings less than 5 seconds, or, ignore timings
less than 10 minutes. Our seek bar has a linear range from 0 to 24. Dealing with a
linear range like that is easy. That's the default behavior. We want something a
bit more complicated. When the user drags a slider, it will go up in 5 second
increments. When they get to 60 seconds, we want to show one minute instead.
From there, we'll go up in one minute intervals up to 10 minutes. After that,
we'll go up in 15 minute intervals, up to 45 minutes. That may sound complicated,
but it's surprisingly easy to do. We just have to set up an array that represents
the real values we want, for each of the values from 0 to 24. An array is ideal
for that, so let's see the code. I'll set the seek bar to value 7, before going back
to our code. Remember that's the 8th dot. We're about to change the data that's
saved, and I want to show you that this can cause a problem, but the problem
doesn't mean our code's not working. I'll click OK to save the new value,
then go back to our code.
I'm going to paste in the array declaration with a comment that shows
the index position of each value.
It's useful to have the index positions above the values, just make sure they
line up correctly. Okay, we're working in seconds. The final value, 2,700 seconds,
is 45 minutes. When we get a value from the seek bar, we can use the value from this
deltas array, to get the amount of time that it represents. The change to
SaveValues is easy. We save the value from this deltas array.
That's straightforward. If the seek value is 7, for example, we save 35 seconds. Next, we've got a
conversion to do, when setting the value of the seek bar. If we read back 35 from
the settings, we need to convert that to 7 before setting the seek bar's value.
We're setting the seek bars initial value on the onViewStateRestored.
So that's where we convert the seconds back into an index.
Arrays have a binary search function that we can use to find the index
position of a value. If the value exists in the array, we get its index position,
otherwise the function returns a negative number.
Note the binary search relies on the array being sorted. Our values are in
order, which is fine, but it wouldn't work if
they weren't in order. All our values have to exist. They were looked up in the
array before being saved, but just in case we've made a mistake somewhere, we
throw an exception if the number of seconds wasn't found. That's going to be
easy to test, as you'll see in a moment. Alright, we're just about done.
The final step is to set the seek bar to the correct value.
If you remember, we set the seek bar's maximum value in the layout. Now you can
see where the value 24 came from. It's the maximum index value of our deltas
array, but it's not a good idea to rely on setting the maximum in the layout.
If we change our array to have more or fewer values, then the code, either
wouldn't use the additional values, or it would crash with an index out of bounds
exception. To prevent that, we set the maximum value explicitly in our code, on
line 16. If the deltas array changes size, the seek bar's maximum will change too.
Okay, let's run the app and watch a crash.
When we've seen it crash, I'll explain why. I'll make sure the logcat's visible, then
clear it before going to the settings.
And the app crashes. Don't worry, there's nothing wrong with our code. Checking the
logcat before the crash, we can see that the value 7 was read in for ignoreLessThan.
7 isn't one of the values in our Delta's array, and our code throws an
exception as a result. Click the link in the stack trace to check that the
exception was the one we threw, in on ViewStateRestored. Our code's working
fine. The problem happens because we're now trying to interpret the saved data
differently. This will happen when you're developing your app. It's very easy to
create invalid data when testing early versions. The fix is easy; uninstall the
app, delete all it's saved data and run it again. I'll do that, and this time we
won't get a crash.
Alright, I'll finish by dragging the slider to about halfway again, and set
the first day to Tuesday. When I tap OK, we should see the values being logged in
logcat; 3 for Tuesday and however many seconds I selected. In the next video,
we'll update the display to show the number of seconds in the dialog.
See you in that one.