11

I've got a DialogPreference which implements a simple TimePicker.OnTimeChangedListener (see below). Setting the time by clicking the +/- buttons works great. But I don't know how to save the state of the timepicker when the user typed in the time directly into the textfield. It could be sufficient to access to the current textfield value, so I'd be able to persist it in onDialogClosed. But timePicker.getCurrentHour() won't do it. Please help...

public class TimePreference extends DialogPreference implements
        TimePicker.OnTimeChangedListener {
// ...
@Override
public void onTimeChanged(TimePicker view, int hours, int minutes) {
    selectedHours = hours;
    selectedMinutes = minutes;
}

@Override
public void onDialogClosed(boolean positiveResult) {
    if(positiveResult) {
        String timeString = selectedHours + ":" + selectedMinutes;
        if(isPersistent()) {
            persistString(timeString);
        }
    }
}
// ...
}
cody
  • 6,389
  • 15
  • 52
  • 77
  • The problem is that onTimeChanged isn't called until the text in the TimePicker looses focus. Does anyone know how to access the TimePicker input text directly? – cody Oct 26 '10 at 14:53
  • You can check my answer here http://stackoverflow.com/questions/7527138/timepicker-how-to-get-am-or-pm/23931797#23931797 – Mukesh May 29 '14 at 11:07

2 Answers2

48

I hadn't noticed this problem until i stumbled upon this question. There is a simple solution: When the user is done changing the date and time in my app, i simply call finish() and in the onPause() or onStop() or onDestroy() I do this:

// force the timepicker to loose focus and the typed value is available !
timePicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
time.hour   = timePicker.getCurrentHour();
time.minute = timePicker.getCurrentMinute();

After this i store the time.toMillis(false) value in the appropriate column of my table.

I don't know if the timepicker is still accessible in your onDialogClosed(boolean positiveResult). If not, find another callback to use when it still is.

Hope this helps.

Larphoid
  • 1,005
  • 8
  • 13
  • Thanks... clear focus works! :-) It can all be done in onDialogClosed. Great! – cody Nov 09 '10 at 19:01
  • 1
    This answer, truly saved my life. – eitama Oct 09 '11 at 01:14
  • Can u please explain the part of the calling finish()? I didn't understand how do u call finish and where do u do it? – Alex K Dec 29 '12 at 09:42
  • for example when Ok is pressed in a dialog. in the OnClick() of the dialog you can timePicker.clearFocus(); and finish(); then in onDialogClose or onPause or onStop or onDestroy you can get the values from the Timepicker. – Larphoid Jan 01 '13 at 19:01
1

Only a very bad workaround so far...

ViewGroup v = (ViewGroup) timePicker.getChildAt(0);
ViewGroup numberPicker1 = (ViewGroup) v.getChildAt(0);
ViewGroup numberPicker2 = (ViewGroup) v.getChildAt(1);
String hours = ((EditText) numberPicker1.getChildAt(1)).getText().toString();
String mins = ((EditText) numberPicker2.getChildAt(1)).getText().toString();

selectedTime = hours+":"+mins;
cody
  • 6,389
  • 15
  • 52
  • 77