1
00:00:03,730 --> 00:00:08,830
G'day everyone, welcome back. The next
improvement to our SettingsDialog, is to

2
00:00:08,830 --> 00:00:12,849
give it a title. We could have put a
title in the layout of course.

3
00:00:12,849 --> 00:00:18,580
That's easy, which is why I didn't do it.
Setting a title for a Dialog should be

4
00:00:18,580 --> 00:00:24,160
easy, and before API 23, it was. Unfortunately,

5
00:00:24,160 --> 00:00:30,400
Google changed the default value in API
23, and just setting the title doesn't

6
00:00:30,400 --> 00:00:35,680
work on devices running marshmallow or
above. That change has also been made in

7
00:00:35,680 --> 00:00:41,440
the Android X libraries. If you've
switched to those, rather than AppCompat,

8
00:00:41,440 --> 00:00:46,140
and we recommended that you do, then the
title won't display on older Android

9
00:00:46,150 --> 00:00:52,150
devices, either. So let's see how to do it
properly. The first step is to add a call

10
00:00:52,150 --> 00:01:05,860
to the dialog's SetTitle function. I'll do
that in onViewCreated. The dialog won't

11
00:01:05,860 --> 00:01:10,840
be null by the time onViewCreated is
called, but we still need to use the safe

12
00:01:10,840 --> 00:01:15,520
call operator. I've used the same string
resource that provides the text for the

13
00:01:15,520 --> 00:01:21,220
menu which seems to be a sensible thing
to do. If the menu item says settings,

14
00:01:21,220 --> 00:01:26,679
then that's a good title for the dialog.
Okay, I'm not gonna run the app because

15
00:01:26,679 --> 00:01:30,909
it's not going to work. To make it work,
we need to override the style for the

16
00:01:30,909 --> 00:01:38,609
dialog. We do that in the Styles dot XML
file in res slash values.

17
00:01:55,530 --> 00:02:01,320
It's that windowNoTitle attribute
that's changed, from defaulting to false,

18
00:02:01,320 --> 00:02:05,780
to now defaulting to true. This style sets
back to false

19
00:02:05,780 --> 00:02:11,520
I used Theme.AppCompat.Light.Dialog.Alert as the parent, because our

20
00:02:11,520 --> 00:02:18,390
AppTheme on line four is Theme. AppCompat.Light.DarkActionBar. If you

21
00:02:18,390 --> 00:02:22,350
used a different theme as the AppTheme,
then the parent for the dialog style

22
00:02:22,350 --> 00:02:28,230
should reflect that. The next step is to
apply the style to our dialog. We do

23
00:02:28,230 --> 00:02:38,480
that back in SettingsDialog, in the onCreateFunction.

24
00:02:38,480 --> 00:02:49,820
I'll generate that before onCreateView. Make sure you
generate the right function, because

25
00:02:49,830 --> 00:03:05,800
there are quite a few that start with onCreate, and it's easy to choose the wrong one.

26
00:03:05,849 --> 00:03:11,010
Alright, that's all we need to do. As I
said, we could have set the title in our

27
00:03:11,010 --> 00:03:15,480
layout, but I wanted to show you this way
so you don't waste hours trying to work

28
00:03:15,480 --> 00:03:20,939
out why your title doesn't appear, if you
want to set it in code. I'll run the app to

29
00:03:20,940 --> 00:03:32,640
make sure it works, and there's the title
on our SettingsDialog. In the next video,

30
00:03:32,640 --> 00:03:36,239
I want to show you how to prevent a
fragment from being destroyed, when its

31
00:03:36,239 --> 00:03:41,239
activity gets destroyed. See you in the
next one.

