AFAIK, there is no "visibility" option for the settings, which makes sense if you think that this is just a ListView.
See [ 1 ]. For what I see, you can do something like this:
PreferenceScreen screen = this.getPreferenceScreen(); // Use "1" since you're using "1" to create it. CheckBoxPreference ckbox = (CheckBoxPreference) this.findPreference("1"); screen.removePreference(ckbox);
To recreate, you can do this [ 2 ]:
screen.addPreference(ckbox);
Also, be sure to create your preferences using setOrder(int order)
so that when you recreate it, it will be recreated in the correct position.
As you can see, it may be appropriate to maintain a global preference link to make it simpler and faster.
Of course, I do not need to say that you should integrate this logic into your CheckboxPreference listener. See this answer by none other than Reto Meyer himself to see a good way to do this (this is also a check box). There, it registers the listener full screen and checks what preference has caused the listener, but you can make it easier (but in more detail later) by simply setting it to setOnPreferenceChangeListener
.
* edit: I see that you also use the button to add preference. You can also implement the same logic above in the button itself. It all depends on whether you want to do this using a check box or button.
Finally, it would be worth it to simply set the on state if you are not doing something like โsee advanced settingsโ or something that is worth keeping novice users from having to do dangerous things for your application. But overall, inclusion states work better for users, IMHO.
Hope this answers your question.