Permalink
Browse files

T06.10- EditTextPreferenceConstraints

  • Loading branch information...
GinaHsu committed Aug 24, 2017
1 parent 8f3ad76 commit 9c14eb2cce91cd70d4abd530785581e21189cd17
@@ -29,7 +29,7 @@
// TODO (1) Implement OnPreferenceChangeListener
public class SettingsFragment extends PreferenceFragmentCompat implements
OnSharedPreferenceChangeListener {
OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener {
@Override
public void onCreatePreferences(Bundle bundle, String s) {
@@ -51,7 +51,10 @@ public void onCreatePreferences(Bundle bundle, String s) {
setPreferenceSummary(p, value);
}
}
// TODO (3) Add the OnPreferenceChangeListener specifically to the EditTextPreference
Preference preference = findPreference(getString(R.string.pref_size_key));
preference.setOnPreferenceChangeListener(this);
}
@Override
@@ -93,6 +96,29 @@ private void setPreferenceSummary(Preference preference, String value) {
// to a float check that that float is between 0 (exclusive) and 3 (inclusive). If it isn't, show
// an error message and return false. If it is a valid number, return true.
// using the onPreferenceChange listener for checking whether the size setting was set to a valid value.
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast errorInput = Toast.makeText(getContext(),
"Please enter a number between 0.1 and 3.", Toast.LENGTH_LONG);
String sizeKey = getString(R.string.pref_size_key);
// Double check that the preference is the size preference
if(preference.getKey().equals(sizeKey)){
try{
float size = Float.parseFloat((String)newValue);
if (size > 3 || size <=0 ){
errorInput.show();
return false;}
}catch (NumberFormatException nfe){
errorInput.show();
return false;
}
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

0 comments on commit 9c14eb2

Please sign in to comment.