How to create Master Switch in preferences setting activity - android

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

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.

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

Related

How create a settings menu with checkbox Android

I want to create a preference screen in which there are three checkboxes; the first one is clickable and the other two are not until the first is checked.
How can I do this? I've seen this tutorial, but there is only one checkbox. Can anyone help me?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="#string/summary_category"
android:title="#string/title_category">
<CheckBoxPreference
android:key="main"
android:defaultValue="true"
android:summary="#string/summary_main"
android:title="#string/title_main"
/>
<CheckBoxPreference
android:key="firstDependent"
android:summary="#string/summary_firstDependent"
android:title="#string/title_firstDependent"
android:dependancy="main"
/>
<CheckBoxPreference
android:key="secondDependent"
android:summary="#string/summary_secondDependent"
android:title="#string/title_secondDependent"
android:dependancy="main"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.
Now create a folder named xml in res folder and put your preferences xml file inside that.
Then do the following.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.
Hope this helps.
You have to do it like in that example, but you'll have three checkboxes instead of one. If you want two checkboxes to be disabled until the first one is true you can use the android:dependency property. With this property you need to specify the key of the preference they will depend on.
<PreferenceCategory
android:summary="..."
android:title="..." >
<CheckBoxPreference
android:defaultValue="true"
android:key="first"
android:summary="#string/summary_first"
android:title="#string/title_first" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="second"
android:summary="#string/summary_second"
android:title="#string/title_second" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="first"
android:key="third"
android:summary="#string/summary_third"
android:title="#string/title_third" />
</PreferenceCategory>

List from SQLite in a preferences.xml

i'm a new android developer and i hope that you guys could help me.
I'm developing a preference screen like above:
<PreferenceCategory android:title="#string/security_title" >
<EditTextPreference
android:name="Password"
android:defaultValue=""
android:key="passwordPref"
android:password="true"
android:summary="#string/security_sumary"
android:title="#string/security_addPassword" />
<PreferenceScreen
android:key="secondPrefScreenPref"
android:summary="Click here to go to the second Preference Screen"
android:title="Second Preference Screen" >
<ListPreference android:entries="#android:id/list" android:key="listPasswordKey" />
</PreferenceScreen>
</PreferenceCategory>
In a password field,the user are going to type his passw and when he clicks OK button, the program are gonna save it in my database. This part is ok!
But when he clicks on a second pref screen i want to list, from database, all your passwords saved.
I know how to load the data, but i don't know how to call my method when the user clicks on a SecondPrefScreen and how to show them in a preferences.xml
can anyone help me?
thx
You will have to create a widget that will load the data and list it for you. Essentially, create an object that extends DialogPreference and give it a child list view that will list your security credentials.

Set a check box to false in Preference Activity

I am trying to implement user preferences. A problem I have is that if one of my check boxes is ticked I want to set the other to false. I know about dependency and from what I have seen it doesn't change the value. Just disables you from changing the preference. What can I do to change a checkbox to false if another is true? This is my xml:
<PreferenceCategory android:title="Launch Settings">
<CheckBoxPreference
android:key="enable_nearest_station"
android:title="Nearest station"
android:defaultValue="false"
android:summary="Will launch your nearest station on startup"
android:disableDependentsState="true"/>
<CheckBoxPreference
android:key="enable_default_station"
android:title="Default station"
android:defaultValue="false"
android:dependency="enable_nearest_station"
android:summary="Allows you to choose a default to launch with."
/>
/>
</PreferenceCategory>
Thanks in advance.
Use the set setOnPreferenceChangeListener(Preference.OnPreferenceChangeListener onPreferenceChangeListener) to your CheckBoxPreference and code to set false other CheckBoxPreference.

Multi select ListPreference on android

Any idea on implementing a multi-select (check boxes) ListPreference on Android?
Would I have to extend the ListPreference?
Is there any classes already documented to do this?
Thanks
Multi select ListPreference now comes natively with Android from API level 11 (Honeycomb).
http://developer.android.com/reference/android/preference/MultiSelectListPreference.html
Because it will be quite a while before devices have Honeycomb or later installed I'd recommend people to stick with the http://blog.350nice.com/wp/archives/240 solution.
EDIT: I think at this moment in time (almost 3 years after this answer was originally posted) you are better off using the native version now as the majority of devices have Android 4 and up.
Well , http://blog.350nice.com/wp/archives/240 does provide a solution , but a simpler solution would be just implementing a child preference screen inside the parent , and then the child preference screen can have multiple check boxes . I know , its not the best solution , but gets the job done .
For eg - the Below preference.xml
<PreferenceCategory
android:title="Regular messages"
android:key="regular_messages">
<CheckBoxPreference
android:key="enable_regular_messages"
android:summary="Enable or disable regular messages"
android:title="Send regular messages"
android:defaultValue="true"
/>
<ListPreference
android:key="send_interval"
android:title="Send interval"
android:summary="Define how often you want to send messages"
android:defaultValue="60000"
android:entries="#array/send_interval"
android:entryValues="#array/send_interval_values"
android:dependency="enable_regular_messages"
/>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Messages type"
android:key="messages_type"
android:summary="Select the type of messages to be sent"
android:dependency="enable_regular_messages">
<CheckBoxPreference
android:key="enable_status_messages"
android:summary="Enable or disable status messages"
android:title="Send status messages"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="enable_event_messages"
android:summary="Enable or disable event messages"
android:title="Send event messages"
android:defaultValue="true"
/>
<CheckBoxPreference
android:key="enable_critical_messages"
android:summary="Enable or disable critical messages"
android:title="Send critical messages"
android:defaultValue="true"
/>
</PreferenceScreen>
</PreferenceCategory>
Found a very useful link:
http://blog.350nice.com/wp/archives/240
Here's a single-class implementation with defaultValue support:
https://github.com/yanchenko/droidparts/blob/develop/droidparts/src/org/droidparts/widget/MultiSelectListPreference.java
There is a github project just for this

Why doesn't my preferences page show the current values?

I have a preferences page which is defined by XML - including some default values. I use a PreferenceActivity to display and handle this page. Whenever I use this page to set the preferences the preference file on the file system is updated properly - I can see this via adb.
However, whenever I go back to the settings page after have changed some of the settings, it's the defaults that are shown. Worse than that, if I press back without changing any settings, it then sets the, all back to the default.
Any ideas on how I can get the prefs to actually show the current settings?
My PreferenceActivityis created thus:
#Override
protected void onCreate(Bundle savedInstanceState) {
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this);
setDefaults(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:id="#+id/numberOfYearsList"
android:key="numberOfYears"
android:title="Number of Years to Read the Bible"
android:summary="How many years would you like to take to read through the reading plan?"
android:entries="#array/numberOfYears"
android:entryValues="#array/numberOfYears"
android:dialogTitle="How Many Years?"
android:defaultValue="1"
/>
<CheckBoxPreference android:key="ignoreDates"
android:id="#+id/ignoreDatesCheckbox"
android:title="Ignore Dates"
android:summary="Would you like to use the dates in the plan?"
android:defaultValue="false"
/>
</PreferenceScreen>
Nevermind - I did a mistake. As you can see from above, when creating the Preference Activity I'm making a call to a method that sets all the prefs to defaults... Duh!

Categories

Resources