I am fairly new to inflating things in Android, and am creating my first Settings screen for my App.
I want my Settings/Preferences screen to have a "master" On/Off Switch in the top-right of the ActionBar, which "greys-out" the settings when "Off", or allows them to be changed when "On"..
I have searched but can't quite figure it out.. Can anybody provide me with a quick rundown of how this can be accomplished simply?
Thanks in advance!

- 578
- 5
- 12
2 Answers
There is an attribute that can be added to a preference item, android:dependency. This attribute determines on which preference this item depends on. Put
android:dependency="idOfSwitchPreference"
on all the items that you'd like depending on the switch preference. When the switch is off, the preferences with the specified dependency will be grayed out. Obviously, replace 'idOfSwitchPreference' with the id of the preference that you'd like the others to depend on.
Check out the documentation for Preference items for more details.

- 737
- 4
- 15
-
Upon creating a new "Settings Activity" and exploring, I now see what you are speaking of.. This would be perfect, but the difference is that i'm trying to put the "Master Switch" inside the ActionBar, rather than inside the Settings menu itself.. How can this be achieved? – Studio2bDesigns Feb 02 '19 at 10:23
-
First thing that comes to mind, include a custom toolbar in the preference activity with a switch item in it. Also create a master switch item in the preferences, but set it's visibility to gone. Wire the switch from the action bar with the hidden preference switch, so that when you toggle the switch in the action bar it also toggles the hidden preference switch. Set the dependencies of all the preferences you want to gray out to the hidden preference item. So, when you change the switch in the toolbar, you change the hidden preference and consequently you disable/enable the other preferences. – deluxe1 Feb 02 '19 at 10:32
I would have the switch connected to a global variable, onCheckChanged() method for the On/Off switch would toggle that Boolean value true or false, when you load up the Settings check that’s global variable and enable/disable and grey out everything depending on the result

- 1,158
- 3
- 12
- 22