7

first question. long time searcher.

i implemented a timePicker preference through expansion of DialogPreference as in the android docs. all works well and i can set the summary through onSharedPreferenceChanged() and also in the override of DialogPreference onSetInitialValue().

my goal is to always have the summary shown with its value and some other strings from resources.
for instance in my ListPreference i have:

android:summary="@string/pref_list_camera_summary"

which resolves to:

<string name="pref_list_camera_summary">Use camera: %s</string>

in my timePreference %s doesn't get expanded. i've searched around and i can't figure out how to get the dialogPreference/timePreference to do this. i can set the summary manually in onSetInitalValue() like this:

setSummary(getSummary() + " " + DateFormat.getTimeFormat(getContext()).format(new Date(calendar.getTimeInMillis())));

i just don't like implied expansion like that. doesn't feel as clean or use the android %s paradigm.

i'll start digging through the listPreference code and see how i can add this to dialogPreference. any ideas/head-starts would be great!

thanks!

aminography
  • 21,986
  • 13
  • 70
  • 74
er0ck
  • 113
  • 7

2 Answers2

5

If you want to replace the preferred string with %s defined in android:summary automatically, I think there is no such possibility. You should handle it programmatically in initialization of your preference. Have you tried to use String.format() to avoid + concatenation?

String date = DateFormat.getTimeFormat(getContext())
        .format(new Date(calendar.getTimeInMillis()));

setSummary(String.format(Locale.getDefault(), 
        getContext().getString(R.string.pref_list_camera_summary), date));
aminography
  • 21,986
  • 13
  • 70
  • 74
  • 1
    The problem is that `getSummary()` is not called in the first place. If you can somehow tweak the derived preference class to make sure it gets called you could indeed use the `string.format()` trick. – l33t Dec 28 '18 at 19:24
  • 2
    If you want to initialize the summary in the preference activity/fragment, it is a common practice to do it in onResume method. Another way is to do the initialization tasks in `onBindViewHolder` of your custom preference class. It is feasible to call `getSummary()` method there. – aminography Dec 28 '18 at 20:51
  • 1
    In my case, the binding is **null** when `onBindViewHolder(PreferenceViewHolder holder)` method is called from the preference overview. Hence, there is no way to get hold of the value that should replace the `%s` container. – l33t Jan 03 '19 at 00:14
  • Not sure what to share. Given that the summary is set to `%s` it is desirable for a derived `DialogPreference` to **override** `getSummary()` and format the text accordingly. The first problem is that `getSummary()` is not even called. Obviously, the derived class needs some logic which I assume somebody out there has implemented since 2014. Hence the bounty :) – l33t Jan 03 '19 at 21:17
  • I have created a custom `DialogPreference` and override the `getSummary()` method. It is called correctly and shows the overriding summary in preferences. You can share summary-related parts of your custom preference class. – aminography Jan 04 '19 at 10:45
3

If you use library Material Preference, you can do it easily:

listPreference.summaryFormatter = { entry, value -> "Value for $entry is $value" }

It will set your ListPreference's summary to something like Value for Weekly is 7.

I know that this answer is not strongly related to your question. But at least, you have found a library that can do it. With Material Preference, you don't have to set %s to the ListPreference's summary.

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87