How create a settings menu with checkbox Android - 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>

Related

How to create Master Switch in preferences setting activity

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

Android checkboxpreference disabled even though it's enabled in XML

I currently have the problem of a disabled checkbox preference which is enabled in the preference.xml. This happened after the migration to Metarial design. I tried to enable the view programmatically but it didn't help.
(I'd like to show a screenshot but my rep is not high enough yet :/)
my preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Network Service" >
<CheckBoxPreference
android:enabled="true"
android:key="#string/communication_option"
android:selectable="true"
android:defaultValue="true"
android:shouldDisableView="false"
android:summary="Allows down- and upload."
android:title="Enable server communication" />
<CheckBoxPreference
android:disableDependentsState="false"
android:enabled="true"
android:dependency="#string/communication_option"
android:key="#string/wifi_sync_option"
android:selectable="true"
android:defaultValue="true"
android:shouldDisableView="true"
android:summary="Do not use mobile bandwidth to download"
android:title="WiFi synchronization only" />
</PreferenceCategory>
</PreferenceScreen>
my preferenceFragment
public class FragmentPreference extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
// added to make sure it really is enabled
CheckBoxPreference cbp = (CheckBoxPreference) getPreferenceManager()
.findPreference(getString(R.string.communication_option));
cbp.setEnabled(true);
cbp.setShouldDisableView(false);
}
I am using the Theme.AppCompat.Light.NoActionBar theme to be able to add the new toolbar.
An other thing i noticed is, that the preference is shown as enabled it the screen orientation changes to landscape and even though the preference is shown as disabled the preference can still be clicked/checked/unchecked.
After a few trial and error I found out that I just overloaded the Preferences.
The slimmer and much cleaner version that works looks like this:
<PreferenceCategory android:title="Network Service" >
<CheckBoxPreference
android:key="#string/communication_option"
android:defaultValue="true"
android:summary="Allows down- and upload."
android:title="Enable server communication" />
<CheckBoxPreference
android:dependency="communication_option"
android:key="#string/wifi_sync_option"
android:defaultValue="true"
android:summary="Do not use mobile bandwidth to download"
android:title="WiFi synchronization only" />
</PreferenceCategory>
and
public class FragmentPreference extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
You have to set the default values in you your main activity's onCreate.
PreferenceManager.setDefaultValues(this, R.xml.pref_general, false);

Android - Preference screen not showing preferences

I have a Preference Activity
But, whenever I call it, the preferences are empty. No title or summaries are visible. You can click on the preference and it will highlight but there is no text. I have tried adding an EditTextPreference and in that case an edit text will pop up on click but there is no text.
Activity:
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="SomePref"
android:summary="Somethingsomething"
/>
</PreferenceScreen>
Thanks
You may try to add this inside PreferenceScreen and see what happens:
<PreferenceCategory
android:title="Preference Category">
<CheckBoxPreference
android:defaultValue="True"
android:key="checkbox_key"
android:title="CheckBox Title"
android:summaryOn="Checkbox is ticked"
android:summaryOff="Checkbox is not ticked" />
</PreferenceCategory>
I figured it out. I'm using the ActionBarSherlock library and I was extending the regular PreferencesActivity when it should have been Sherlock*PreferencesActivity
Everything works great now.
Always, throughout the app save your preferces through getApplicationContext(), if you use this as context it will save with activity context and cannot retreived all the time

how to return to the previous PrefenenceScreen from nested PreferenceScreen without press the back button

I have write a multi-level Preferences in program with xml File. and i want to know how to return to the previous level without press the back button.
how to write some code to implement an item in preference to return previous Preferences level.
I had the same problem and solve it thanks to Xuelong and getDialog() but without needing to manage onPreferenceTreeClick().
You need to keep an instance (myPreferenceScreen) of the PreferenceScreen you want to return from
You have to give him a key in XML
Retrieve the instance with findPreference("MyPreferenceScreenKey");
Once you have to return , use this method : myPreferenceScreen.getDialog().dismiss()
You will then return from where you came from.
Here is a epurated example :
Xml file :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:key="contactList"/>
<PreferenceScreen android:title="My Sub Preference Screen ..."
android:key="mySubScreenKey">
<EditTextPreference android:key="foo1"/>
</PreferenceScreen>
</PreferenceScreen>
Java file :
public class ParanoidPreferenceManager extends PreferenceActivity {
ListPreference contactList;
EditTextPreference foo1;
PreferenceScreen mySubScreenKey;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Load XML preference file
addPreferencesFromResource(R.xml.preferences);
contactList = (ListPreference) findPreference("contactList");
foo1= (EditTextPreference) findPreference("foo1");
screenContact = (PreferenceScreen) findPreference("screenAddContact");
foo1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mySubScreenKey.getDialog().dismiss();
return false;
}
});
}
}
That's it
Sorry for presentation, this is my first post on this site.
Bye dudes
Building on Patrice's and Xuelong's answers, I have made the following functions to easily do the job:
// Refresh the content of a PreferenceScreen by simulating a click (thus it will call the refresh code if it's contained inside the click callback)
private void refreshPreferenceScreenByClick(String prefScreenName) {
// Refresh preference screen (given by its name) by calling the click callback
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
Preference.OnPreferenceClickListener click_callback = prefScreen.getOnPreferenceClickListener();
click_callback.onPreferenceClick(prefScreen);
}
// Close the current PreferenceScreen (or any given the name)
// Useful to go back to the previous PreferenceScreen when constructing dynamically nested submenus.
private void closePreferenceScreen(String prefScreenName) {
PreferenceScreen prefScreen = (PreferenceScreen) findPreference(prefScreenName);
prefScreen.getDialog().dismiss();
}
So first if what you do in your submenu define what is shown on the parent menu (eg in a music app: submenu allows to add a new instrument, and the parent menu shows the list of added instruments), you first have to call refreshPreferenceScreenByClick("parentPreferenceScreen"), and then to go back to the parent menu you can just close the current submenu by calling closePreferenceScreen("currentPreferenceScreen").
Alternatively, if you want to just open a submenu programmatically, you can use the openPreference function found here (tested and it works well): https://stackoverflow.com/a/4869034/1121352
NOTE: do NOT use the openPreference() function to go back to parent PreferenceScreen, because after a few iterations you will get a stack overflow and your app will crash (because Android will keep in memory the history of all previous menus, including the ones you open via openPreference()!).
You can simulate the press of the back button:
this.onBackPressed();
or
getActivity().onBackPressed();
if you are in a fragment.
I think you are looking for something like this
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="NotifSettings" android:title="Notification Settings">
<PreferenceCategory android:title="Notification Settings">
<CheckBoxPreference android:key="NotifyOption"
android:title="Notification Status" android:defaultValue="true"
android:summaryOn="Notifications are Enabled." android:summaryOff="Notifications are Disabled."
android:persistent="true" />
<Preference android:title="XXXX"
android:dependency="NotifyOption" android:summary="xxxxxx"
android:key="Xxx" />
</PreferenceCategory>
<PreferenceCategory android:title="YYYYY">
<PreferenceScreen android:title="Configure Notifications"
android:summary="yyyyy">
<CheckBoxPreference android:key="aa"
android:title="aaaaa" android:defaultValue="true"
android:summary="aaaa" />
<CheckBoxPreference android:key="bb"
android:title="bbbbb" android:defaultValue="true"
android:summary="bbbb." />
...
<ListPreference android:title="zzz"
android:summary="zzzz" android:key="zz"
android:defaultValue="0" android:entries="#array/OverDesc"
android:entryValues="#array/OverValues" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title="Alert Type">
<CheckBoxPreference android:key="AlertVibr"
android:title="Vibrate" android:defaultValue="true"
android:summaryOn="Vibration Turned ON" android:summaryOff="Vibration Turned OFF" />
<CheckBoxPreference android:key="AlertSound"
android:title="Sound" android:defaultValue="true" android:summaryOn="Sound Turned ON"
android:summaryOff="MUTE" />
</PreferenceCategory>
But what I didnt get is that your want to return to the previous preference screen, without pressing back button. Can you shed some more light on that??

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.

Categories

Resources