Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have modified the code from first answer:</p> <ul> <li>it stores selected time in long form (milliseconds) which is easier to work with (using Calendar) then string</li> <li>it automatically shows selected time in summary field in user's format (12 or 24 hour)</li> </ul> <p>Updated code:</p> <pre><code>public class TimePreference extends DialogPreference { private Calendar calendar; private TimePicker picker = null; public TimePreference(Context ctxt) { this(ctxt, null); } public TimePreference(Context ctxt, AttributeSet attrs) { this(ctxt, attrs, android.R.attr.dialogPreferenceStyle); } public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) { super(ctxt, attrs, defStyle); setPositiveButtonText(R.string.set); setNegativeButtonText(R.string.cancel); calendar = new GregorianCalendar(); } @Override protected View onCreateDialogView() { picker = new TimePicker(getContext()); return (picker); } @Override protected void onBindDialogView(View v) { super.onBindDialogView(v); picker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); picker.setCurrentMinute(calendar.get(Calendar.MINUTE)); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult) { calendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour()); calendar.set(Calendar.MINUTE, picker.getCurrentMinute()); setSummary(getSummary()); if (callChangeListener(calendar.getTimeInMillis())) { persistLong(calendar.getTimeInMillis()); notifyChanged(); } } } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return (a.getString(index)); } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { if (restoreValue) { if (defaultValue == null) { calendar.setTimeInMillis(getPersistedLong(System.currentTimeMillis())); } else { calendar.setTimeInMillis(Long.parseLong(getPersistedString((String) defaultValue))); } } else { if (defaultValue == null) { calendar.setTimeInMillis(System.currentTimeMillis()); } else { calendar.setTimeInMillis(Long.parseLong((String) defaultValue)); } } setSummary(getSummary()); } @Override public CharSequence getSummary() { if (calendar == null) { return null; } return DateFormat.getTimeFormat(getContext()).format(new Date(calendar.getTimeInMillis())); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload