Edit Text Preference

github: T06.09_EditTextPreference

resource: EditTextPreference

* Reference: Udacity Nanodegree Program- Become an Android Developer *

  • Add and EditTextPreference with all of the appropriate strings stored in strings.xml. It should have a key, default value of 1 and title.
  • Modify the setupSharedPreferences method and onSharedPreferencesChanged method to properly update the minSizeScale, assuming a proper numerical value is saved in shared preferences.
  • Add code here to properly set the summary for an EditTextPreference.
<!-- TODO (1) Add and EditTextPreference with all of the appropriate strings stored in strings.xml.
    It should have a key, default value of 1 and title -->
<EditTextPreference
    android:defaultValue="@string/pref_size_default"
    android:key="@string/pref_size_key"
    android:title="@string/pref_size_label"/>

/res/values/strings.xml

<!-- Label for the edit preference -->
<!-- Key for the edit preference -->
<!-- default for the edit preference -->
<string name="pref_size_label">Size Multiplier</string>
<string name="pref_size_key" translatable="false">size</string>
<string name="pref_size_default" translatable="false">1</string>
// TODO (2) Modify the setupSharedPreferences method and onSharedPreferencesChanged method to
// properly update the minSizeScale, assuming a proper numerical value is saved in shared preferences
private void setupSharedPreferences() {
    // Get all of the values from shared preferences to set it up
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    .............
    loadSizeFromSharedPreferences(sharedPreferences);
    loadColorFromPreferences(sharedPreferences);
    // Register the listener
    sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(getString(R.string.pref_show_bass_key))) {
        mVisualizerView.setShowBass(sharedPreferences.getBoolean(key, getResources().getBoolean(R.bool.pref_show_bass_default)));
    } else if (key.equals(getString(R.string.pref_show_mid_range_key))) {
        mVisualizerView.setShowMid(sharedPreferences.getBoolean(key, getResources().getBoolean(R.bool.pref_show_mid_range_default)));
    } .....
    } else if(key.equals(getString(R.string.pref_size_key))){
        loadSizeFromSharedPreferences(sharedPreferences);
    }
}
private void setPreferenceSummary(Preference preference, String value) {
    // TODO (3) Don't forget to add code here to properly set the summary for an EditTextPreference
    if (preference instanceof ListPreference) {
        // For list preferences, figure out the label of the selected value
        ListPreference listPreference = (ListPreference) preference;
        int prefIndex = listPreference.findIndexOfValue(value);
        if (prefIndex >= 0) {
            // Set the summary to that label
            listPreference.setSummary(listPreference.getEntries()[prefIndex]);
        }
    }else if(preference instanceof EditTextPreference){
        //For EditTextPreferences, set the summary to the value's string representation.
        preference.setSummary(value);
    }
}

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s