Search code examples
androidstringpreferencespreferenceactivityon-the-fly

How to you change the value of android:summary on-the-fly using coding?


I have the following in a settings.xml file:

<PreferenceCategory android:title="@string/chiming_activity_time_category_title">

    <Preference android:title="@string/chiming_start_time_title"
        android:summary="@string/chiming_start_time_summary" android:key="preferences_start_time" />
</PreferenceCategory>

And this is from the strings.xml file:

<string name="chiming_start_time_summary">Allow chiming to be active STARTING from 

Can you tell me how you change the value of android:summary on-the-fly using coding?

I need to have it display different values based on what the user selects in the preferences screen.

Thanks.

Truly, Emad


Solution

  • Took a look at the documentation? setSummary
    You can find your Preference in the PreferenceActivity with findPreference

    Documetation of PreferenceActivity shows you how setup your activity (which you probably did).

    Do something like that on end of onCreate:

    findPreference("keyOfPreferenceWhichChanges").setOnPreferenceChangeListener(
            new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
           if (somethingChanged) {
                findPreference("keyOfPreferenceWithNewSummary").setSummary("here's the change!");
           }
        }
    }