Android: support %s expansion in DialogPreference summary
Asked Answered
S

2

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!

Spondaic answered 15/3, 2014 at 16:20 Comment(0)
T
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));
Topdress answered 28/12, 2018 at 13:35 Comment(5)
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.Carloscarlota
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.Topdress
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.Carloscarlota
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 :)Carloscarlota
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.Topdress
H
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.

Hime answered 30/12, 2018 at 20:54 Comment(1)
It's a DialogPreference :/Carloscarlota

© 2022 - 2024 — McMap. All rights reserved.