I'd like to create a preference field called Interval
and I want to be able to popup a TimePicker
and set a mm:ss
formated value with minimal value 00:30
and step 30 seconds.
Is it possible to use TimePicker
in PreferenceScreen
?
I'd like to create a preference field called Interval
and I want to be able to popup a TimePicker
and set a mm:ss
formated value with minimal value 00:30
and step 30 seconds.
Is it possible to use TimePicker
in PreferenceScreen
?
I have modified the code from first answer:
Updated code:
For those whom the implementation of a custom Preference isn't so obvious (like it wasn't for me), you have to add this to your
preferences.xml
or whatever you're calling it.You'll end up with something like this:
Assuming you added the TimePreference to your own root package:
(
src/com/example/myapp/TimePreference.java
)For
Preferences Support Library
different code is needed. It requires two custom classesTimePreference
andTimePreferenceDialogFragmentCompat
, as well as overide ofonDisplayPreferenceDialog
method inPreferenceFragmentCompat
extension class.TimePreference.java
TimePreferenceDialogFragmentCompat.java
Required modifications in
PreferenceFragmentCompat
extension classWith above code time preference can be used in preferences xml file like this
CommonsWare's solution has a few problems, which I fixed:
Here's my code, enjoy.
Like LEO87, I was seeing ClassCastException's. The problem was due to stale persisted data from a previous control of the same name. Possible solutions are to clear the app data, use a different name (key), or if you must use the same key name, catch the exception as follows:
add this for Summary:
and add
to end of onSetInitialValue and onDialogClosed.