Time Zone: UTC

How to set the previous selected value in TimePicker?

I mean, click on the TextView, the TimePicker dialog will appear, then I select the time and set it to the TextView, for example. 12:30 on TextView.

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

How can i do this?

+3
source share
1 answer

If you create a time dialog using onCreateDialog and call it, it will automatically save 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 display a dialog box.

+3
source

All Articles