G'day everyone. Welcome back. Our Settings Dialog will extend the appCompatDialog
Fragment class. Create a new Kotlin class called
SettingsDialog, and choose class before hitting OK. I'll add the usual log tag
and extend the appCompatDialogFragment class.
The first step is to inflate our layout in the onCreateView function.
I'll get Android studio to generate the stub, then inflate our layout. I'll also log the
function being called.
Note that we're using onCreateView, rather than oncreateDialog. In the
appDialog class we used a dialog builder in the onCreateDialog function. Here, we're
inflating the view from a layout resource file in onCreateView.
Be careful if you mix the two functions in the same class. You can get unexpected
problems. We need to access the individual widgets, which is why we're
inflating a view in onCreateView, rather than using a dialog builder. Alright, we
want to set up the onClickListeners for our buttons, which we'll do in the
onViewCreated function. I'll get Android Studio to generate that
one as well, after onCreateView.
I'll write the save values function later. If the user taps the OK button, we'll save
the values and dismiss the dialogue. If they tap cancel, we'll just dismiss the
dialogue. A dialog isn't going to do a lot at the moment but we can test it.
Firstly, we add code in MainActivity, in the onOptionsItemDelected function.
Uncomment this line and replace the true.
Alright, we can run the app and test our settings dialog. When I choose settings
from the menu, our dialog appears. The day that starts the week can be selected
from the spinner widget, which drops down to show all the days we defined in the
string array. I can also drag the seek-bar left and right, but we're not getting the
value to show yet and nothing's being saved. But our basic settings dialog is
working. We've still got a bit more work to do.
It would look a lot better with a title too. I'll tap OK to dismiss the dialogue,
and we'll continue with the code in the next video.
I'll see you there.