G'day everyone, welcome back. The next improvement to our SettingsDialog, is to
give it a title. We could have put a title in the layout of course.
That's easy, which is why I didn't do it. Setting a title for a Dialog should be
easy, and before API 23, it was. Unfortunately,
Google changed the default value in API 23, and just setting the title doesn't
work on devices running marshmallow or above. That change has also been made in
the Android X libraries. If you've switched to those, rather than AppCompat,
and we recommended that you do, then the title won't display on older Android
devices, either. So let's see how to do it properly. The first step is to add a call
to the dialog's SetTitle function. I'll do that in onViewCreated. The dialog won't
be null by the time onViewCreated is called, but we still need to use the safe
call operator. I've used the same string resource that provides the text for the
menu which seems to be a sensible thing to do. If the menu item says settings,
then that's a good title for the dialog. Okay, I'm not gonna run the app because
it's not going to work. To make it work, we need to override the style for the
dialog. We do that in the Styles dot XML file in res slash values.
It's that windowNoTitle attribute that's changed, from defaulting to false,
to now defaulting to true. This style sets back to false
I used Theme.AppCompat.Light.Dialog.Alert as the parent, because our
AppTheme on line four is Theme. AppCompat.Light.DarkActionBar. If you
used a different theme as the AppTheme, then the parent for the dialog style
should reflect that. The next step is to apply the style to our dialog. We do
that back in SettingsDialog, in the onCreateFunction.
I'll generate that before onCreateView. Make sure you generate the right function, because
there are quite a few that start with onCreate, and it's easy to choose the wrong one.
Alright, that's all we need to do. As I said, we could have set the title in our
layout, but I wanted to show you this way so you don't waste hours trying to work
out why your title doesn't appear, if you want to set it in code. I'll run the app to
make sure it works, and there's the title on our SettingsDialog. In the next video,
I want to show you how to prevent a fragment from being destroyed, when its
activity gets destroyed. See you in the next one.