Hey there.
So we created 0 0 list preference but there is a small problem when we check or remove the check mark
sound disabled is displayed when the E check sound enabled.
Now the time a mail order setting does not show which Saunders said that is so we can change the sound
but it is not feasible which sound set and this is a little wrong.
We need to reflect what kind of salt is sent here.
Paul's preference.
It is a bit more complicated done for chequebooks preference.
We have here not in value but to our race with names and values.
Well what is the word anyway.
We have to do this implement the settings fragment in the own create preference method here.
The following code share preferences.
And then we get them.
Why a preference screen.
We need to get preference screen.
And after that specify get shared preferences.
Next let's create a preference screen object and get it using the get preference screen method.
Then create the variable int count and assign the value preference screen get preference count to get
the number of settings that are on the preference screen.
That is in this case we will have a checkbox preference.
And here 3 settings in the least preference will total for.
And then we can get a specific setting using iteration.
This is done as follows.
For int i equals zero.
I lost the count by plus plus and here we create the object preference
select the library with on
and assign the value that we get from the preference screen from this object for reference screen and
get preference and specify the index of this preference by.
And then you need to create a helper method.
Private void.
I will call it said preference or label
and I will pass two parameters here.
The first is the preference and the second string value
so in the beginning of this method we check if preference instance or at least preference
this one parameter that will be the input should be exactly at least preference have to be.
And then we will execute the following quote We create the list preference object and assign eat the
class value of our preference or parameter.
We immediately cast to the same class.
Next we create the variable in the index and assign a list preference fine index of value and pass this
simple parameter value.
Next I check in the index value is valid.
If index more than or equal to zero in this case I said.
Summary
here I write this preference.
Get Andres and here I specify index not entry entries.
This method returns an array.
So here we have the index indicated in square brackets.
And so what happens in the set preference label method.
We check first where the preference is at least preference and if so then we create a new object of
that list preference.
Class and an integer variable of type index assign the value that we get from these three string.
Value at is this is value that is in preference time values and then by this value more precisely by
the index obtained for this value we first get all the rare cuts from the salary from the label sorry
and set by the index.
Specifically this entry we can use this helper method in our court and we'll have iterated through all
the settings that are in the preference screen.
As you remember in addition to list preference here is checkbox preference.
We don't need checkbox preference in this case.
Therefore we should excluded in this code and therefore we should check a preference instance or J box
preference where we put this expression in brackets
and put the exclamation sign before that this means not here we check if this setting is not checkbox
preference.
Only then we will execute this code there.
If these checkbox preference then the code for it will not be executed next right.
String value and assign the value that we can get from the file share preferences in which we will access
the one hand the object share preferences get string.
The key we get from our object preference preference get key and then the default value specify an empty
string.
Then use our how bar method that we have just created.
Right.
So preference label best here first parameter object preference.
And the second parameter the value which we have just received.
Well let's run it and now and see how it works.
And as you can see we all on Syria installed here and if we change to Bell nothing changes here but
if we exit from the settings and again enter to this settings activity then the label has changed.
Why is this happening.
This happens because we set this court so on create preferences.
That is it is executed when a sentence fragment is created so that the changes are displayed instantly.
We need to set that on shared preference change.
Listen.
Let's do this.
First we need to implement on shared preference change listener interface.
And as you see it is underlined in red.
We need to implement methods or rather while method.
The only one on shared preference changed.
It appears here below as you can see here there are two promoters in this method trade preferences and
key.
Let's create a variable object of type preference and assign a value using font preference method and
specify the key parameter here that is we will find this setting by key.
Next we check each preference instance of checkbox preference the same quote that I wrote more.
Here we put the exclamation sign.
It means not that we exclude checkbox preference.
And in this case we create the string value and assign to the value obtained from the shared preferences
file and get string method preference get.
And the default value an empty string.
Next again we call our help a method so preference label at best here preference and value.
In addition to implementing the method now we also need to guess to our listener.
We need to do this in their own create method.
Let's redefine it or override it control all on great select and write the full and quote get preference
screen get shared preferences and a register on shared preference.
Change listener as the parameter pass these and just like that when the activity is destroyed we need
to remove it from the registration.
Do this in the destroy method control law again create and override the method method on destroy.
We write the same code get preference screen get shared preferences but only our guests on shared preference
change listen.
This that is.
Here we are a guest on shared preference.
Change listener.
And here we are.
Register it in the on destroy method only by mistake.
I specified on destroy you over read and of course on destroy should be specified.
Well now let's round up and see if we succeed
so
colleague time and melody.
Well Sarah and as you can see on Sera appears immediately.
Let's beep said beep
Okay.
It slows down a little.
Let's go again.
Cancel all siren.
One more time.
Beep Everything turns on Boyle belting song correctly.
Let's check if this song works.
Exactly.
These
yes it was.