So as you can see here this is the line of code that I pasted here and here I referred to the name of
this class preference manager.
OK so this class is going to actually manage the preferences in our Android application.
OK so for example example when a user wants to actually change the settings of the application we need
to actually use this class free France manager in order to manage these sitting.
So this is very important that we actually use this glass.
So prevents manager that set default values.
So what does this method do this.
If it actually is going to actually specify the default values for the first time that our application
is actually installed inside our application.
So unhallowed majorly closed this application and now let's actually uninstall and install this application
here.
OK.
And let's actually run our project again and let's install our project here.
Our application on this emulator here again.
So as you can see here this is our application and this is for the first time that our application is
actually running on this emulator.
So now we need to actually read some of the actually preferences some of the settings that we initially
assigned to this quiz underline preferences that exempt.
So insight is always underlying prefer preferences that is.
And as you can see we have a list preference or multi multi-select preference and other and other less
preference.
And finally another list preference.
So as you can see here for each of these components as you can see I provided a default value right.
A default value for example for the colors are provided the black color for the font.
I provide the default font and for other components I also provided a default value.
So why do they do that.
Because for the first time that our application is actually running on the user's device or application
the Android device is going to actually read the default values for the components for these radio buttons
or for these for these checkboxes and the default values is going to be for example determine the background
color of the quiz.
OK.
So that default value for the first time is really important because the user is actually running the
application for the first time.
And we must actually specify the default value for example for the backend on color or for the front
of the text of the buttons or for example how many options we want to show to the user in order to actually
guess the name of the animal or for example we may we may want to we may want to specify the type of
the animals that we want to actually put inside the squeeze.
So this is very important that we specify a default value and inside this main activity that double
file here as you can see that Savoye we use this class your preference manager in order to set those
default values inside this uncreate method of this main activity that Java class as you can see.
So preference manager that set default values and dismiss that as she accepts free arguments.
Right.
The first argument is is going to be the context.
So the context is this class right main activity that this.
So this is going to be the context that we are going to actually set the default values and for the
second argument we need to specify the actual preferences file that we provided inside our project so
that this class is going to read what those default values from this exemplified so are that examine
that cuius underline preferences so this is the name of this class that we of this vitamine file that
we created inside our project right.
So our That is XML that is underlying preferences at the end is deferred argument.
So what does this argument do.
Now let me show you that for example if you actually call this method again a preference manager that
set default values and then as you can see it is going to show us the arguments so as you can see here
the first argument is actually of type Boolean right and the assigned value falls for the first argument.
Now it says read again.
So the name of this argument is read again.
And what does this mean.
This means that that for the first time our application is running on the user's device and our the
and relativize is going to actually delete those default values from this quiz Underland preferences
that is am I right.
So what happens when the user actually is running the application for actually multiple times for the
next time.
Right.
For example one hour later or for example 10 days later.
In that case as you can see we dont want the application we dont want the device the Android device
to lead to Leto's the default values from there.
This is underlined preferences that are similar because they use it may have actually changed those
values.
OK to use it may have actually to change the background color of the query or use that a man may actually
changed.
I mean have actually changed the front of the text of the buttons or the user may have actually changed
kind of animals that he or she wants to be used in say the quiz or the user may have actually specified
that.
How many options how many guesses are going to be inside the quiz.
So those preferences those settings are going to actually are going to be saved in say the shirt preferences
inside another class.
But here as you can see be assigned a value for us and this means that we dont want to read again after
the user has actually installed the application on the device and they use it for example is running
the application on the device.
And for the next time that the user is actually in the application on the device we dont want to read
preferences to settings from these cuius underling preferences again.
So thats why we assign de-value falls for the first argument which is a Boolean read again right.
We just want to read those default values for the first time that the user is actually running the application
and so forth.
And for the next times of you're going to actually provide some other values and then I'm going to show
you that.
But for now I just know that we assign the value false for the read again argument.
So now let me paste in some lines of code here.
Inside this uncreate my feet.
And then I'm going to explain to you what's going on here.
So as you can see here these are the lines of code that I pasted here.
So as you can see here I refer to the name of this class preference manager that get default shirt preferences.
Right.
Here we want to get the shirt preferences of our application inside the user's device.
So this is really important because when ever the use that is actually changing the settings of the
quiz animal or get it they're going to actually save those changes to these shared preferences.
So it's very important that we get this shows preferences and as you can see we need to pass this argument
main activity that these.
So this is going to be the context.
Right.
And as you can see I am calling an other method and these are shared preferences.
Right.
So main activity that these dot legis there on shared preferences change the center.
So what that is what does this mean.
This means that we are going to create our listener for the changes of our settings.
Right.
So diskless main activity is going to be notified when ever the user is actually changing the settings
of the application.
So now let me show you Let me show you that in say the emulator.
So now as you can see here our application is running on the simulator.
And if I click on the Settings button here now for example if I want to actually change the background
color of the Queen for example change it to white.
Now as you can see here it says new changes are applied and this means that this class main activity
here is going to be notified when ever the settings is changed.
For example whenever the background color is changed all the fault of the texts of the guest buttons
are actually changed or of for example the types of animal is actually changed.
Right.
Or we can actually change the number of guesses.
So this means that here you are actually creating a listener inside this main activity class to those
changes.
We are going to be notified to those changes so that we can actually respond to those changes.
So now if I click on dispatch button here as you can see now the background color is white and this
means that the main activity class is actually notified to that change and the background color is white
according to that setting according to the change that the user has actually made here in say the settings
activity.
So hopefully that makes sense and its very important that we create our listener for this.
Listen for this shirt preferences.
So preference managers that get default shirt preferences so here we are actually getting the shared
preferences and then we are calling a method on this show preferences.
So that register on shared preferences change listener and then if you are passing this listener to
dismiss it.
So we havent created this listener yet inside this class so we need to create this listener inside this
class settings change listener right.
So now let's actually copy this settings change the center here and now let's scroll down here and then.
Now inside this class.
So before the end in calibrates of these class classes as you can see we have this mean activity the
class and before the end in curly brace of this class now we're going to create a listener for that
for those changes in say the settings activity.
Right.
A shared preferences change listener.
This is really important.
So here just typing private shared preferences dot on shared preferences change listener.
And then here we need to specify a name for this listener right.
So just paste it pistis value here.
Settings change the listener and then put an assignment operator here new again shared preferences that
as you can see should be Finance's dot on shirt preferences change Listen here and now just open and
close the listener by by putting an opening curly brace followed by a closing curly brace.
So now it may actually create a new line here and now we can actually see disvalue here.
Private shared preferences that aren't shared.
Preferences change the listener.
So here we are going to create a listener to those changes.
Inside these shows preferences and in Amy's settings changed the center right.
The same name that be specified here inside is on create Smurfit settings change to listener right.
So now I put an assignment operator here new shared preferences that aren't shared preferences but that
unshipped preference change listener.
Right so now we are inside this inside the curly braces unopen opening curly brace followed by a closing
curly brace and now as you can see here we have an error.
So here we are actually creating an anonymous inner class right.
I told you about anonymous classes in Java in the previous tutorials and now you know what our anonymous
classes.
And here you are creating an anonymous class that actually conforms to this interface.
As you can see here unshared friends change to the center and now you need to actually provide the abstract
method that we have inside this interface and shared to prevent a strange listener.
So what is that an abstract Meffert that abstract.
Is that abstract.
If it is an shared preference change listener.
So if you need to write this move it here and at the end as you can see if we have this incredible piece
of this listener right we need to put up a semicolon here and as you can see here the error is gone
so we override it.
This method here inside these anonymous inner class.
So here we have an anonymous class.
Right and this anonymous class is actually conforming to this on a shared friends change listener interface.
And because that anonymous in a class is actually conforming to this interface we need to actually provide
the abstract method that we have inside this interface right.
So here as you can see we have this method here inside these anonymous class as you can see that it
isn't if it is actually public Woit on shared preference changed.
So this is going to be called whenever it is the use that is actually has changed the preferences of
the application.
So as you can see here we have these preferences.
So for example the user change the format.
And if I click on this back button he had as he can't see the font is changed right.
So this is going to be called when ever the use of changes to preferences.
So on shirtsleeve friends change and the it accepts two arguments the first one is shared preferences
and the second one is the key right.
So now later we will be actually putting more logic's inside this method for now just put these lines
of code here.
And now let's actually scroll to the top here.
As you can see now the error is gone right.