Set Value of a CheckBoxPreference in an Activity - android

Hello I need to know how to set a value programmatically.
I am using that code
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
.
.
.
SharedPreferences.Editor geted = prefs.edit();
geted.putBoolean("checkBox_Schedule", false);
geted.commit();
But I dont see anything change
The code of my xml for my checkboxPreference is
<CheckBoxPreference
android:defaultValue="false"
android:dependency="checkBox"
android:key="checkBox_Schedule"
android:summary="On/Off"
android:title="Schedule" />
One solution is to do
startActivity(new Intent(SetPreference.this, SetPreference.class));
But this is not what I want to do.

CheckBoxPreference showContact = (CheckBoxPreference)findPreference("myPreference");
showContact.setChecked(false);

You can call this in your preference activity
CheckBoxPreference pref = (CheckBoxPreference)findPreference("example_pref_key");
pref.setChecked(false);

Related

Change style using button and save this state

I have two color styles for my app.
I can change them using button, but i haven't got idea how I could save actually style as domestic. When I restart my app, I have other style than I choose.
Have You got idea how I can save this state? Maybe savedInstanceState?
Thank's for any reply
Have you thought about persisting the changes? You can use SharedPreferences just like that:
// While choosing new style
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("style", styleId);
editor.commit(); // commit changes
// While retriving choosen style
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
int styleId = pref.getInt("style", null); // null is default value
setStyle(styleId); // custom method

How to get setting value from VolumePreference

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);

boolean SharedPreferences always loading default values

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);

How can i save value of theme set by user to retrieve it later?

This is what i am doing to save value of drawables:
case R.id.purple:
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.purple);
button1 = buttoncos = buttonmadd = R.drawable.purple;
}
editor.putInt("DigitButtonStyle",button1);
editor.putInt("MemoryButtonStyle", buttonmadd);
editor.putInt("FunctionButtonStyle", buttoncos);
editor.commit();
return true;
Drawables here are integeral values so it was easy.How do i store values of different themes.
Easy way to do this, is SharedPreferences
Save the relevant value:
SharedPreferences shared=getSharedPreferences("theme", Activity.MODE_PRIVATE);
shared.edit().putString("theme_name", "THEME_BLUE").commit();
Retrieve the saved data:
SharedPreferences shared=getSharedPreferences("theme", Activity.MODE_PRIVATE);
String theme=shared.getString("theme_name", null);
For more info: link
Read this setTheme in this page, it tells that you cannot use in middle of an activity or in other words, has to be done before setting any theme to your activity.

Using logical NOT with android:dependency from preferences

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;
}
});

Categories

Resources