1

I have integrated TimePickerFragment with the help of DialogFragment & TimePickerDialog.OnTimeSetListener. When I click on TextView it calls/opens this TimePickerFragment.

Success Cases :

1] When I scroll anytime e.g 11:19 & press OK (TimePicker) then it sets the correct time 11:19

2] When I click on time (hour/minute) and edit using soft keyboard e.g. 04:10 & then press soft keyboard ENTER button & then OK (TimePicker) then it sets the correct time 04:10

Failed Case:

1] When I click on time(hour/minute) & edit using soft keyboard e.g. 08:44 & then NOT PRESSING SOFT KEYBOARD ENTER button & then OK(TimePicker) then IT SETS the wrong time ANY EARLIER SET TIME e.g.10:35 or something else

Below is my code:

//inside on click
    case R.id.textViewEtoa:
        DialogFragment newFragment = new TimePickerFragment(getActivity(), this);
        newFragment.show(getActivity().getSupportFragmentManager(), "timePicker");
        break;

//inside TimePickerFragment class
    public class TimePickerFragment extends DialogFragment
    implements TimePickerDialog.OnTimeSetListener {

        private Context context;
        private OnTimeSetListener onTimeSetListener;

        public TimePickerFragment(Context context, OnTimeSetListener onTimeSetListener) {
            // TODO Auto-generated constructor stub
            this.context = context;
            this.onTimeSetListener = onTimeSetListener;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            int hour, minute;
                final Calendar c = Calendar.getInstance();
                 hour = c.get(Calendar.HOUR_OF_DAY);
                 minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // Do something with the time chosen by the user
            System.out.println(minute + " ...hhour.... " + hourOfDay);
            onTimeSetListener.onTimeSet((hourOfDay < 10 ? "0" + hourOfDay : "" + hourOfDay),
                    (minute < 10 ? "0" + minute : "" + minute));
        }

    }
halfer
  • 19,824
  • 17
  • 99
  • 186
VVB
  • 7,363
  • 7
  • 49
  • 83
  • I am not sure, but it seems the EditText for hour or minute still has the focus on it, when you press the OK on the TimePicker and NOT the keyboard. Try giving focus to any other view, on the click of OK on the TimePicker. – Eric B. Nov 19 '15 at 06:22
  • If we consider but still CALLBACK should return correct time. which is not returning – VVB Nov 19 '15 at 06:45
  • It is not returning because the value doesn't gets committed in the EditText. – Eric B. Nov 19 '15 at 06:51
  • But OK button event handles itself by TimePicker, there is no code written on that. – VVB Nov 19 '15 at 07:13
  • @Puru Pawar Please mark your comment – VVB Nov 20 '15 at 05:05
  • It's a regression in `lollipop`. Looking for workaround too – basin Jul 10 '16 at 16:49

0 Answers0