How do I get Timepickerdialog to start with the current time?

Asked

Viewed 585 times

3

I made a TimePickerDialog, but I can’t get him to start the current time, always calls 00:00. I’d like to know where I’m going wrong.

Code:

private void chamarTimePickerDialog() {
    TimePickerDialog.OnTimeSetListener  mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hora, int minuto) {
            Toast.makeText(getActivity(), hora+ ":" + minuto, Toast.LENGTH_SHORT).show();
            capHora.setText(hora+ ":" +minuto);


            Calendar calNow = Calendar.getInstance();
            calNow.setTimeInMillis(System.currentTimeMillis());
            calNow.set(Calendar.HOUR_OF_DAY, hora);
            calNow.set(Calendar.MINUTE, minuto);

        }
    };
    TimePickerDialog dialog = new TimePickerDialog(getActivity(), mTimeSetListener,0, 0, true);

    dialog.show();
}
  • Lari, I changed the title of your question to make it easier for you to find it. Take advantage and make a tour also to learn more about the site.

  • thank you cat! rs

1 answer

4


You can specify the initial time and minute at the time of construction of the object Timepickerdialog.

See the statement of builder:

TimePickerDialog (Context context, 
                TimePickerDialog.OnTimeSetListener listener, 
                int hourOfDay, 
                int minute, 
                boolean is24HourView)

hourOfDay and minute is the time and minutes with which the Timepickerdialog is initialized.

You are passing zero to each of these parameters,

TimePickerDialog dialog = new TimePickerDialog(getActivity(), mTimeSetListener,0, 0, true);

hence be initialised with 00h00

Change the code so it stays that way:

private void chamarTimePickerDialog() {
    TimePickerDialog.OnTimeSetListener  mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hora, int minuto) {
            Toast.makeText(getActivity(), hora+ ":" + minuto, Toast.LENGTH_SHORT).show();
            capHora.setText(hora+ ":" +minuto);


            Calendar calNow = Calendar.getInstance();
            //Não é necessário, getInstance já retorna o calendário com a data e hora actual
            //calNow.setTimeInMillis(System.currentTimeMillis());
            calNow.set(Calendar.HOUR_OF_DAY, hora);
            calNow.set(Calendar.MINUTE, minuto);

        }
    };

    //Cria um calendário com a data e hora actual
    Calendar calNow = Calendar.getInstance();
    TimePickerDialog dialog = new TimePickerDialog(getActivity(),
                                                   mTimeSetListener, 
                                                   Calendar.HOUR_OF_DAY,
                                                   Calendar.MINUTE,
                                                   true);

    dialog.show();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.