http://developer.android.com/reference/android/preference/Preference.html#attr_android:dependency
if I want my list to be dependent from other check preference named on_off I can do this
<ListPreference
android:defaultValue="100"
android:dependency="on_off"
android:dialogTitle="text"
android:entries="#array/display_values"
android:entryValues="#array/entry_values"
android:key="preferences_text"
android:summary="text"
android:title="text" />
and every time when the check preference on_off have value false this list preference will be disabled.
But what If I want everytime when on_off is true the list preference to be disabled ?
I need something like logical NOT for the android:dependency
is this possible ?
In preference XML, set android:disableDependentsState attribute of the "on_off" CheckBoxPreference to true. E.g.:
<CheckBoxPreference
android:disableDependentsState="true"
android:key="on_off"
android:summaryOff="Dependent is enabled"
android:summaryOn="Dependent is disabled"
android:title="Checkbox that disables its dependents state in reverse manner" />
You can implement it in the code:
chb = (CheckBoxPreference) findPreference("chb");
list = (ListPreference) findPreference("list");
chb.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
list.setEnabled(!chb.isChecked());
return false;
}
});
Related
For the application I am working on, I need to have a preference screen, which has a EditTextPreference, SwitchPreference and a VolumePreference. I am using the VolumePreference as I need a preference that is set with a slider, and VolumePreference was the only one I could find that fit the bill. Here is my Preference XML:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:capitalize="words"
android:defaultValue="#string/pref_default_channel"
android:inputType="number"
android:key="pref_channel"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/pref_title_channel"
android:summary="#string/pref_summary_channel"/>
<SwitchPreference
android:key="pref_customVol"
android:title="#string/pref_title_custom_vol"
android:summary="#string/pref_summary_custom_vol"
android:defaultValue="false" />
<VolumePreference
android:name="Volume Preference"
android:title="Notification Volume"
android:summary="Set your notification volume, so you will be notified when your Seahorse is almost finished running"
android:key="pref_notifVolPref"
android:dependency="pref_customVol"/>
I am able to get the value set by the SwitchPreference easily from shared preferences, using the code:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean customVol = sharedPref.getBoolean("pref_customVol", false);
However, when I try to do the same to get the value from my VolumePreference, it always returns the default value of -1. Here is the code I use to retrieve the volume preference:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int vol = sharedPref.getBoolean("pref_notifVolPref", -1);
Am I using the VolumePreference incorrectly? Do I need to create my own custom preference to display a slider preference? Has anyone had this issue before? Thanks
first extends PreferenceActivity
second add this to onCreate()
addPreferencesFromResource(R.xml.account_preferences);
Preference checkPreference = getPreferenceScreen().findPreference("checkbox_contacts_sync");
Preference.OnPreferenceChangeListener lis1 = new Preference.OnPreferenceChangeListener()
{
#Override
public boolean onPreferenceChange(Preference preference, Object o)
{
Toast.makeText(SystemManager.getAppContext(), "yess", Toast.LENGTH_LONG).show();
return true;
}
};
checkPreference.setOnPreferenceChangeListener(lis1);
and add this to layout
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#android:id/list">
</ListView>
Or in main activity use this.
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.OnSharedPreferenceChangeListener shListener = new SharedPreferences.OnSharedPreferenceChangeListener()
{
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key)
{
Assistance.print("change");
Toast.makeText(SystemManager.getAppContext(),"ohh",Toast.LENGTH_LONG).show();
}
};
sharedPreferences.registerOnSharedPreferenceChangeListener(shListener);
I've a strange issue with SharedPreferences and boolean.
I've set this code in my xml:
xml:
<CheckBoxPreference
android:key="onlywifiupload"
android:defaultValue="true"
android:summary="#string/summary_onlywifiupload"
android:title="#string/title_onlywifiupload"
/>
and from the Java code, I'm calling:
boolean onlywifiupload = pref.getBoolean("onlywifiupload", true);
Even the checkbox is checked or unchecked, in onlywifiupload there's always true.
Same with setting:
boolean onlywifiupload = pref.getBoolean("onlywifiupload", false);
It seems the default value is always loaded instead of checked values.
It seems the only way to make it working is:
mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
onlywifiupload = mPrefs.getBoolean("onlywifiupload", true);
don't know why I need to call getDefaultSharedPreferences from PrefenceManager object
Before that, I was calling the preferences in this way:
pref = getSharedPreferences("AppPref", MODE_PRIVATE);
Hey everyone, I'm trying to make an activity so that when a CheckBox Preference is enabled (true), programmatically the on-screen keyboard InputType will change. I want the user to have the option to turn on/off the keyboard's email address option while keeping the inputType's already set.
Essentially doing the equivalent of changing:
android:inputType="textAutoCorrect|textCapSentences|textMultiLine"
to
android:inputType="typeAutoCorrect|textCapSentences|textmultiLine|textEmailAddress"
Preferences.xml
<CheckBoxPreference android:key="pref_key_enable_email"
android:title="#string/pref_title_enable_email"
android:summary="#string/pref_summary_enable_email"
android:defaultValue="false" />
This is what I have as of now.
public void setInputType(int type) {
boolean showEmail = false;
// Show the Email keyboard if the pref_key_enable_email preference is TRUE
mTextEditor = (EditText) findViewById(R.id.embedded_text_editor);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean("pref_key_enable_email",false)== true){
showEmail = true;
if (showEmail) {
mTextEditor.setInputType(InputType.TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS); w
} else {
mTextEditor.getInputType();
}
}
I've checked the /data/data/myappname/shared_prefs com.myappnamehere.preferences.xml. I at least know that the boolean values do change from false to true when the box is checked. It just doesn't do anything :(
Consider cleaning your code a little bit
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean showEmail = prefs.getBoolean("pref_key_enable_email",false);
mTextEditor = (EditText) findViewById(R.id.embedded_text_editor);
if(showEmail) {
mTextEditor.setInputType( mTextEditor.getInputType() | TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
}
I'm assuming you have your "default" flags in the xml for your layout:
android:inputType="textAutoCorrect|textCapSentences|textMultiLine"
I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I have a preferences window with two checkbox and one back button.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="PIN requirement">
<CheckBoxPreference
android:title="Use PIN"
android:defaultValue="true"
android:key="checkboxPref" />
<CheckBoxPreference
android:title="Don't use PIN"
android:defaultValue="false"
android:key="checkboxPref2" />
</PreferenceCategory>
<PreferenceCategory>
<Preference
android:title="Back"
android:key="customPref" />
</PreferenceCategory>
</PreferenceScreen>
How to change two CheckBox in to the RadioButton group?
If you need just to enable or disable using PIN, only one CheckBoxPreference will be enough in this case (see example code below, First Category). RadioButtons are usually used, when you need to choose something from a list of settings (ListPreference) - for example (see example code, Second Category), to pick a color.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="First Category">
<CheckBoxPreference
android:title="Using PIN"
android:defaultValue="false"
android:key="checkboxPref"
android:summaryOn="Disable PIN"
android:summaryOff="Enable PIN"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Second Category">
<ListPreference
android:title="Pick your favourite color"
android:key="listPref"
android:defaultValue="4"
android:entries="#array/listArray"
android:entryValues="#array/listValues" />
</PreferenceCategory>
</PreferenceScreen>
The source code for this example will be:
public class PreferencesHelpExample extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public static final String KEY_LIST_PREFERENCE = "listPref";
private ListPreference mListPreference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Get a reference to the preferences
mListPreference = (ListPreference)getPreferenceScreen().findPreference(KEY_LIST_PREFERENCE);
}
#Override
protected void onResume() {
super.onResume();
// Setup the initial values
mListPreference.setSummary("Current value is " + mListPreference.getEntry().toString());
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// Set new summary, when a preference value changes
if (key.equals(KEY_LIST_PREFERENCE)) {
mListPreference.setSummary("Current value is " + mListPreference.getEntry().toString());
}
}
}
For ListPreference you will also need an arrays.xml file, which is located in the "values" folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listArray">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</string-array>
<string-array name="listValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
</resources>
See also some great examples, working with PreferenceActivity - they helped me a lot:
Android Preferences;
How to create a group of RadioButtons instead of a list;
How to display the current value of an Android Preference in the Preference summary?
In some situations, having Preferences behave as radio buttons is a nice feature e.g a simple way of including summary text under each option.
Making a group of checkboxes behave like a group of radio buttons is fairly simple. To do this in Preferences, register all the checkboxes in the group to the same OnPreferenceChangeListener then in this listener use findPreference() to find the other checkboxes and call setChecked(false);
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
if (key.equals("checkboxPref")) {
//Reset other items
CheckBoxPreference p = (CheckBoxPreference)findPreference("checkboxPref2");
p.setChecked(false);
}
else if (key.equals("checkboxPref2")) {
//Reset other items
CheckBoxPreference p = (CheckBoxPreference)findPreference("checkboxPref");
p.setChecked(false);
}
//Force the current focused checkbox to always stay checked when pressed
//i.e confirms value when newValue is checked (true) and discards newValue
//when newValue is unchecked (false)
return (Boolean)newValue;
}
The only downside is that the checkboxes will not look like radio buttons...
I think this is pretty simple if that is what you want:
Add the checkboxes at the preferenses xml like that:
<CheckBoxPreference
android:key="prefkey_cbp_1"
android:summary="#string/prefkey_cbp_1"
android:title="#string/prefkey_cbp_1"
android:defaultValue="false" />
<CheckBoxPreference
android:key="prefkey_cbp_2"
android:summary="#string/prefkey_cbp_2"
android:title="#string/prefkey_cbp_2"
android:defaultValue="false" />
//etc...
At the preferences class onCreate build an CheckBoxPreference array or list or whatever you like, like that:
ArrayList<CheckBoxPreference> cbp_list = new ArrayList<CheckBoxPreference>();
cbp_list.add((CheckBoxPreference) getPreferenceManager()
.findPreference(PreferencesProtocol.prefkey_cbp_1));
cbp_list.add((CheckBoxPreference) getPreferenceManager()
.findPreference(PreferencesProtocol.prefkey_cbp_2));
Make your preferences class implement OnPreferenceClickListener and set the OnPreferenceClickListener to the checkboxes like that :
for (CheckBoxPreference cbp : cbp_list) {
cbp.setOnPreferenceClickListener(this);
}
Then just override the onPreferenceClick and handle any click. For example if you want checkboxes to perform like radio buttons (in the same radiogroup), meaning, only one checkbox at the time to be check, do something like that:
#Override
public boolean onPreferenceClick(Preference arg0) {
for (CheckBoxPreference cbp : cbp_list) {
if (!cbp.getKey().equals(arg0.getKey()) && cbp.isChecked()) {
cbp.setChecked(false);
}
}
return false;
}
try this:
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, #NonNull Preference preference) {
CheckBoxPreference cb1 = null;
CheckBoxPreference cb2 = null;
ListAdapter adapter = preferenceScreen.getRootAdapter();
for(int i = 0; i < preferenceScreen.getRootAdapter().getCount(); i++)
{
//attention to type and key
Preference pref = (Preference) adapter.getItem(i);
if (pref == null || pref.getKey() == null)
continue;
if (pref.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_akt_position)))
cb1 = (CheckBoxPreference) pref;
if (pref.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_last_location)))
cb2 = (CheckBoxPreference) pref;
if (cb1 != null && cb2 != null)
break;
}
//be safe
if (cb1 == null || cb2 == null)
return super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_akt_position))) {
CheckBoxPreference cb = (CheckBoxPreference) preference;
cb2.setChecked(!cb.isChecked());
}
if (preference.getKey().equals(getString(R.string.tag_preference_key_always_zoom_to_last_location))) {
CheckBoxPreference cb = (CheckBoxPreference) preference;
cb1.setChecked(!cb.isChecked());
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
I have used PreferenceActivity to have preference in my android application. I want one preference say "pref 2" to be enabled when other preference say "pref 1" is NOT checked and "pref 2" to be disabled when "pref 1" is checked.
i.e. exactly opposite of the android:dependancy attribute.
How can I do that?
Yes it's possible to do this out of the box.
Let's say you want to disable pref2 when pref1 is off
Here's the code(preference xml layout) to put in for pref1:
<CheckBoxPreference
android:title="Pref1"
android:key="pref1">
</CheckBoxPreference>
Here's the code(preference xml layout) to put in for pref2:
<EditTextPreference
android:dialogMessage="Pref 2 dialog"
android:title="Pref2"
android:key="pref2"
android:dependency="pref1">
</EditTextPreference>
Like sigmazero13 said, by default disableDependentsState is false so you don't need to include it in the pref1 attributes.
According to the docs here, you can add an attribute to the CheckBoxPreference tag like so:
android:disableDependentsState="true"
By default this is false, which means that the dependents are disabled when the checkbox is unchecked, but by setting it to true, it should have the opposite effect.
I don't think there's any out-of-the-box solution for it, i.e. an inverted dependancy attribute. But there's always the click-listener:
preference1.setOnPreferenceClickListener(pref1_click);
....
private OnPreferenceClickListener pref1_click = new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
preference2.setEnabled(!preference1.isChecked());
return true;
}
}
Android CheckBox??
I am assuming you are using the Android.widget.checkBox:
http://developer.android.com/reference/android/widget/CheckBox.html
Try this
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox1.isChecked()) {
checkBox2.setChecked(false);
}
}
}
GoodLUCK!!