[Android Apps and Games] Wuby986:
hey there!!

let's give an even more helpful "touch" to these posts.. let's explain in details so everyone can learn how it is possible to do something like this..

"regular dependency" -master on, child greyed out - can be set via xml but what in case we need to set master off child on?

so [MENTION=4714483]daxgirl[/MENTION] came up with a nice method that is going to be invoked in the onSharedPreferenceChange method in HandlePreferenceFragments class

in this case we don't set dependency via xml as regular requires

method:

private void setDependencies(boolean isMasterChecked) {
String[] keysArray = {"child_key1", "child_key2", "child_key3"}; //You can set as many keys as you want to be dependent [B]as long as they are all switch preferences.[/B] You can play around with that
for(int i=0; i<keysArray.length; i++) {
[COLOR="Red"]SwitchPreference[/COLOR] dependantSwitch = ([COLOR="Red"]SwitchPreference[/COLOR]) pf.findPreference(keysArray[i]);
dependantSwitch.setChecked(!isMasterChecked);
}
}

this method will be placed somewhere between other methods and is going to be invoked this way for example:

case "SwitchPreference":
SwitchPreference s = (SwitchPreference) pf.findPreference(key);
s.setChecked(sharedPreferences.getBoolean(key, true));
if (key.equals("quick_connect_toggle")) {
appRebootRequired("com.android.systemui");
} else {
}
if (key.equals("toggle_background_enable")) {
appRebootRequired("com.android.systemui");
} else {
}
if (key.equals("custom_carrier")) {
appRebootRequired("com.android.systemui");
} else {
}
if(s.getKey().equals("multi_window_enabled")) {
setDependencies(s.isChecked());
}
break;

so basically we are in this case coding java to pass the current boolean status of the "multi_window_enabled" key in database to the method above that is going to reverse the status ( using " ! " ) and apply it in database to the child key that we put in a string array name keysArray.

this specific method is intended to be applied to a SwitchPreference or CheckBoxPreference (because this way we can pass a boolean value) to one or multiple SwitchPreference.

letìs make a different case.. let's say we need to enable/disable the child - a ListPreference let's say - on reverse status of master.. so master on dependency disabled (because we cannot set to off status since it is a list we can grey it out) and viceversa

we leave same invoke for the method but we change the method..

private void setDependencies(boolean isMasterChecked) {
String[] keysArray = {"child_key1", "child_key2", "child_key3"}; //You can set as many keys as you want to be dependent [B]as long as they are all list preferences.[/B] You can play around with that
for(int i=0; i<keysArray.length; i++) {
[COLOR="Red"]MyListPreference[/COLOR] dependantList = ([COLOR="Red"]MyListPreference[/COLOR]) pf.findPreference(keysArray[i]);
dependantList.setEnabled(!isMasterChecked);
}
}

as you can see it has different preferences highlighted in red, it depends to which kind of preference it has to apply, for this example we say child_key1 and other in list are MyListPreference in xml..

also you can notice that we apply " setEnabled" to dependency, because since is a list it cannot be on/off but only enabled/disabled.

of course we could apply setEnabled to the SwitchPreference as well, if our project requires it or we can apply both if we need it to be disabled and specifically off for example:

private void setDependencies(boolean isMasterChecked) {
String[] keysArray = {"child_key1", "child_key2", "child_key3"}; //You can set as many keys as you want to be dependent [B]as long as they are all switch preferences.[/B] You can play around with that
for(int i=0; i<keysArray.length; i++) {
[COLOR="Red"]SwitchPreference[/COLOR] dependantSwitch = ([COLOR="Red"]SwitchPreference[/COLOR]) pf.findPreference(keysArray[i]);
dependantSwitch.setChecked(!isMasterChecked);
dependantSwitch.setEnabled(!isMasterChecked);
}
}

all of this is based on master beeing a switch preference.. in another case we need to change quite a lot of things actually and it depends on what value we get from the master - string or other-

you can see already some usage of this in the existing code, not for dependency, but you can try to build up a method based on the above ones and in case you have problems, we can have a look

i hope you can find this complete and understandable enough..

have fun!!