Use Resources!

github: T06.04_exercise_UseResources

  • Create Boolean and Strings resources for checkbox preference
  • Update the hardcoded values in the pref_visualizer to the new resources
  • Update the hardcoded values in the VisualizerActivity‘s onCreate method

app/src/main/res/values/strings.xml:

<!-- TODO (1) Add Strings for checkbox preference -->
<string name="pref_show_bass_label">Show Bass</string>
<string name="pref_show_bass_key" translatable="false">show_bass</string>

<string name="pref_show_true">Shown</string>
<string name="pref_show_false">Hidden</string>

app/src/main/res/values/bools.xml :

<bool name="pref_show_bass_default">true</bool>

app/src/main/res/xml/pref_visualizer.xml:

<!-- TODO (2) Create a res->bools.xml file and create a boolean for this default value -->
<!-- TODO (3) Update these values to be values from resource files and not hard coded -->

<CheckBoxPreference
    android:defaultValue="@bool/pref_show_bass_default"
    android:key =  "@string/pref_show_bass_key"
    android:summaryOff="@string/pref_show_false"
    android:summaryOn="@string/pref_show_true"
    android:title="@string/pref_show_bass_label" />

app/src/main/java/android/example/com/visualizerpreferences/VisualizerActivity.java:

// TODO (4) Use resources here instead of the hard coded string and boolean
mVisualizerView.setShowBass(sharedPreferences.getBoolean(getString(R.string.pref_show_bass_key),
        getResources().getBoolean(R.bool.pref_show_bass_default)));

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 )

Google+ photo

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

Connecting to %s