List Preference

github: T06.07_ListPreference

  • Add a list preference
  • Add string values for the list preference
  • Add a res->values->arrays.xml file which contains two arrays, one for labels and one for values. (The arrays should contain strings found in this file)
  • Update setupSharedPreferences and onSharedPreferenceChanged to load the color

<!-- TODO (1) Add a list preference -->
<ListPreference
 android:defaultValue="@string/pref_color_label_blue"
 android:entries="@array/pref_color_option_labels"
 android:entryValues="@array/pref_color_option_values"
 android:key= "@string/pref_color_key"
 android:title="@string/pref_color_label"/>
<!-- TODO (2) Add string values for the list preference -->
<!--Label for the color preference-->
<string name="pref_color_label">Share Color</string>
<!--Lable for colors preference-->
<string name="pref_color_label_red">Red</string>
<string name="pref_color_label_blue">Blue</string>
<string name="pref_color_label_green">Green</string>

<!--Key name for color preference in SharePreferences -->
<string name="pref_color_key" translatable="false">color</string>
<!-- TODO (3) Add a res->values->arrays.xml file which contains two 
arrays, one for labels and one for values. 
The arrays should contain strings found in this file-->
<resources>
 <array name="pref_color_option_labels">
 <item>@string/pref_color_label_red</item>
 <item>@string/pref_color_label_blue</item>
 <item>@string/pref_color_label_green</item>
 </array>

 <array name="pref_color_option_values">
 <item>@string/pref_color_red_value</item>
 <item>@string/pref_color_blue_value</item>
 <item>@string/pref_color_green_value</item>
 </array>
</resources>
// TODO (4) Update setupSharedPreferences and onSharedPreferenceChanged to load the color

// from shared preferences. Call setColor, passing in the color you got
private void setupSharedPreferences() {
    // Get all of the values from shared preferences to set it up
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    mVisualizerView.setShowBass(sharedPreferences.getBoolean(getString(R.string.pref_show_bass_key),
            getResources().getBoolean(R.bool.pref_show_bass_default)));
    mVisualizerView.setShowMid(sharedPreferences.getBoolean(getString(R.string.pref_show_mid_range_key),
            getResources().getBoolean(R.bool.pref_show_mid_range_default)));
    mVisualizerView.setShowTreble(sharedPreferences.getBoolean(getString(R.string.pref_show_treble_key),
            getResources().getBoolean(R.bool.pref_show_treble_default)));
    mVisualizerView.setMinSizeScale(1);
    mVisualizerView.setColor(sharedPreferences.getString(getString(R.string.pref_color_key),
            getString(R.string.pref_color_blue_value)));

    // 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_show_treble_key))) {
        mVisualizerView.setShowTreble(sharedPreferences.getBoolean(key, getResources().getBoolean(R.bool.pref_show_treble_default)));
    }else if(key.equals(getString(R.string.pref_color_key))){
        mVisualizerView.setColor(sharedPreferences.getString(getString(R.string.pref_color_key),
                getString(R.string.pref_color_blue_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