3

How to set previous selected value in TimePicker?

I mean, click on TextView, TimePicker dialog occurs, then I select the time and set it in the TextView, e.g. 12:30 PM on TextView.

After that, if I want to change the time, click again to TextView and then TimePicker dialog should show the previous selected value. So the TimePicker should display 12:30 PM.

How can I do that?

Mat
  • 202,337
  • 40
  • 393
  • 406
Nilesh Verma
  • 914
  • 1
  • 11
  • 26
  • http://stackoverflow.com/questions/3328757/how-to-click-or-tap-on-a-textview-text this should set you straight on the first half of your question –  May 01 '12 at 13:29

1 Answers1

3

If you create Time Picker Dialog with onCreateDialog and call it it will automatically store the previous value.

@Override
protected Dialog onCreateDialog(int id) {

switch (id) {

    case 0:

TimePickerDialog timeDlg = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                            int minute) {
                        // TODO Auto-generated method stub

                        if (hourOfDay > 12) {
                            hourOfDay = hourOfDay - 12;
                            time = " PM";
                        } else if (hourOfDay < 12 && hourOfDay != 0) {
                            time = " AM";
                        } else if (hourOfDay == 12) {
                            time = " PM";
                        } else if (hourOfDay == 0) {
                            hourOfDay = 12;
                            time = " AM";
                        }

                        Toast.makeText(
                                getApplicationContext(),
                                new StringBuilder().append(pad(hourOfDay))
                                        .append(":").append(pad(minute))
                                        .append(time), Toast.LENGTH_SHORT)
                                .show();
                    }
                }, 12, 00, false);

        timeDlg.setMessage("Set Time:");

        timeDlg.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Dismiss",
                        Toast.LENGTH_SHORT).show();
            }
        });
        return timeDlg;
}
    return null;
}

Use showDialog(id); to Show the Dialog.

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62