Android: Show/hide preference on checking/unchecking other CheckBoxPreference - android

Im trying to accomplish such a thing:
when I check CheckBoxPreference 'A' the other preference ('B') shows below the A,
when I uncheck 'A', preference 'B' hides...
So generally speaking it should work just like dependency but not only enabling/disabling th preference B, but hiding it.
This is what i came up with:
prefA = (CheckBoxPreference)findPreference("preference_A");
prefA.setChecked(false);
prefB = findPreference("preference_B");
category.removePreference(prefB);
prefA.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean switchedOn = (Boolean)newValue;
if (switchedOn)
{
Log.d("pref_test", "prefA checked");
category.addPreference(prefB);
}
else
{
Log.d("pref_test", "prefA UNchecked");
prefB = findPreference("preference_B");
category.removePreference(prefB);
}
return switchedOn;
}
});
prefA and prefB have been defined earlier as PreferenceFragment class fields.
The problem is that it works fine only for 2 clicks and my logs say:
prefA checked
prefA UNchecked
prefA UNchecked
Like it was calling onPreferenceChangeListener twice for unchecking (obviously resulting in .removePreference(prefB) method returning null).
Any idea on solving the issue?

Would it not work doing something like this?
prefA = (CheckBoxPreference)findPreference("preference_A");
prefA.setChecked(false);
prefB = findPreference("preference_B");
category.removePreference(prefB);
prefA.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean switchedOn = (Boolean)newValue;
if (switchedOn) {
Log.d("pref_test", "prefA checked");
category.addPreference(prefB);
} else {
Log.d("pref_test", "prefA UNchecked");
prefB = findPreference("preference_B");
category.removePreference(prefB);
}
return true;
}
});

Related

setDefaultNightMode() only works after second call in PreferenceFragment

I am working to implement a dark mode into my app. Right now I try to switch the UI mode between light and dark using a PreferenceFragment nested inside an AppCompatActivity. The App behaves like this, starting from the light theme as a default value:
If I select 'dark' in the ListPreference, the summary of the
preference changes, but the UI stays light in this and all the other activities.
If I select 'dark' a second time, the activity switches to dark theme, as well as the other activities in the backstack.
The same thing happens in reverse, when I want to switch back to the light theme.
So basically everything works, but you have to select the desired value in the ListPreference twice. The code for the ListPreference:
final Preference listPreferenceDesign = findPreference(PREF_DESIGN);
listPreferenceDesign.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
if (((ListPreference) preference).getValue().equals("light")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (((ListPreference) preference).getValue().equals("dark")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
return true;
}
});
What I tried so far:
Call getActivity.recreate()before the return true; statement
Call getActivity.recreate()after a short delay using a Handler
Call this.recreate() in the onResume() method of the parent Activity when a boolean changedDesignSetting was true
I am thankful for further help.
I finally get it to work using an OnSharedPreferenceChangeListener in the parent activity. Now the code in the parent activity goes like this:
getFragmentManager().beginTransaction().replace(R.id.settingsPlaceholderID, preferenceFragment).commit();
SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(PREF_DESIGN)) {
if (sharedPreferences.getString(key, "light").equals("light")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
}
}
};
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
And in the PreferenceFragment I only have:
final Preference listPreferenceDesign = findPreference(PREF_DESIGN);
listPreferenceDesign.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
return true;
}
});

PreferenceActivity - EditTextPreference - on value changed

I have an EditTextPreference (key "userfirstname"). I want to call method when it has been changed. I read documentation of PreferenceActivity and EditTextPreference, but I didn't find relevant solution.
Is there any way to do that ?
Preference userNamePref = findPreference("userfirstname");
userNamePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
//do something
return true;
}
});

How to get updated text value from EditTextPreference - it gets old value

I am trying to setup a new password using EditTextPreference. In this Android 4.0.3, clicking the EditTextPreference opens a dialog window where user can enter text. However, on pressing ok, it still shows the old text value and not the newly entered value.
public class MyPreferencesActivity extends PreferenceActivity {
EditTextPreference edp_password = (EditTextPreference) findPreference("pref_key_account_password");
edp_password.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
public boolean onPreferenceChange(Preference preference, Object newValue) {
String password = edp_password.getText();
Log.v(TAG, "Password is: " + password);
return true;
}
});
I spend a while trying to make it work, but couldn't find any good solution. How can I retrive the newly entered text after user presses Ok.
Try this way:
EditTextPreference pref_dayCount = (EditTextPreference)findPreference("pref_dayCount");
pref_dayCount.setDefaultValue(30);
pref_dayCount.setSummary(getResources().getString(R.string.pref_plan_days_number_summary)+" "+pref_dayCount.getText());
pref_dayCount.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary(getResources().getString(R.string.pref_plan_days_number_summary)+" "+newValue.toString());
return true;
}
});
ok i think i know what the problem is, from the android developers references, it says that this callback is invoked before the internal state has been updated, so i guess that why you see the old value.
the way you did it was fine, but i'm guessing you need to use the newValue object, maybe something like this:
public class MyPreferencesActivity extends PreferenceActivity {
EditTextPreference edp_password = (EditTextPreference) findPreference("pref_key_account_password");
edp_password.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.v(TAG, "Password is: " + (EditText)newValue.getText());
return true;
}
});
i'm not sure if this works, i only free write it :P
password.getText will not get the updated value. Use:
String password = newValue.toString();

Android EditText preference validation

I am using Edittextpreference as one of the preference in settings section. I want to validate this edittextpreference when the user enters data to it and clicks ok; before saving it in sharedpreference.
I am trying to do something like this but this saves the preference first I suppose.
editTextPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (((newValue.toString().length() == 15) {
// save preference only if length is equal to 15
}
})
});
can someone guide me how to validate edittextpreference before it is saved in sharedpreference so that I can decide if I want to save it or not.
According to doc here
Called when a Preference has been changed by the user. This is called
before the state of the Preference is about to be updated and before
the state is persisted.
And it returns True to update the state of the Preference with the new
value.
So you can do the following
editTextPreference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
if (((newValue.toString().length() == 15) {
//
return true;
}
else{
// invalid you can show invalid message
return false;
}
})
});

OnPreferenceChangeListener for every setting

I know that I can do something like this:
Preference pref = findPreference(getString(R.string.pref_vibrate_on_key));
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
LogUtil.d("Working!");
return true;
}
});
But I would like to add a Listener to every preference.
I tried doing:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
LogUtil.d("working!");
}
});
but it doesn't work.
Is this possible? If so, what am I doing wrong?
Assuming you want the same listener called each time, this might be a simpler solution:
Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Code goes here
return true;
}
};
EditTextPreference pref = (EditTextPreference)findPreference(getString(R.string.pref1));
pref1.setOnPreferenceChangeListener(changeListener);
EditTextPreference pref2 = (EditTextPreference)findPreference(getString(R.string.pref2));
pref2.setOnPreferenceChangeListener(changeListener);
I think that onSharedPrefererenceChanged is fired upon saving the preference (when you click BACK or HOME in PreferenceActivity). I think that the easiest way is to create single class implementing OnPreferenceChangeListener and switch through Preference.getKey(); and set it as OnPreferenceChangeListener for each Preference.

Categories

Resources