How to create Master Switch in preferences setting activity

224 views Asked by At

I want to create master switch as shown in image to enable or disable all options using android preference activity.

https://i.stack.imgur.com/fsA8G.png

Something like this :- https://source.android.com/devices/tech/settings/settings-guidelines#master_setting

2

There are 2 answers

2
MaYar On

You can do this in Settings Activity or Fragment. There are a lot of tutorials on YouTube.

New -> Activity -> Settings Activity

1
Ton On

I didn't find a solution either but I have something similar. Maybe it helps.

I have this on top:

<PreferenceCategory app:title="">
    <SwitchPreferenceCompat
        app:key="MasterSwitch"            
        app:summaryOff="Everything is disabled"
        app:summaryOn="Everything is enabled"
        app:title="This is a master switch (more or less)"
        />
</PreferenceCategory>

And then, every PreferenceCategory depends on the above (app:dependency="MasterSwitch"). So, if the "MasterSwitch" is disabled, all categories below are displayed as disabled. Set the Categories this way:

<PreferenceCategory
        app:dependency="MasterSwitch"
        app:title="Another category">

        <EditTextPreference
            app:key="signature"
            app:title="Option 1"
            app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

and so on...

The problem is that I cannot set the nice blue or gray background color that you showed here: https://source.android.com/devices/tech/settings/settings-guidelines#master_setting

I cannot have the information box it is shown there either.