Search code examples
javaandroiddatepickertimepicker

Date/Time Pickers not giving changed values


I created an AlertDialog that contains both a DatePicker and a TimePicker inside of it. The date and time pickers are initialized each time they are opened to whatever the date is on the text of the button that is used to open it. When the user clicks the "Set" button in the dialog box it is supposed to update it the text on the button to the selected date and time but it keeps it at the old date and time.

It appears the using datePicker.getYear() and all those other methods isn't getting the selected data in the picker because I made a Toast to test it and it displayed the old information only.

Here is the code:

startDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //get date from button text
            Calendar timeInButton = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.US);
            try {
                timeInButton.setTime(sdf.parse(startDate.getText().toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int pYear = timeInButton.get(Calendar.YEAR);
            int pMonth = timeInButton.get(Calendar.MONTH);
            int pDay = timeInButton.get(Calendar.DAY_OF_MONTH);
            int pHour = timeInButton.get(Calendar.HOUR_OF_DAY);
            int pMinute = timeInButton.get(Calendar.MINUTE);

            final View dialogView = View.inflate(MyActivity.this, R.layout.date_picker_dialog, null);

            final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
            datePicker.init(pYear, pMonth, pDay, null);

            final TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
            timePicker.setCurrentHour(pHour);
            timePicker.setCurrentMinute(pMinute);

            AlertDialog.Builder dateAndTimePickerDialog = new AlertDialog.Builder(AddCashGame.this);
            dateAndTimePickerDialog.setTitle("Select Starting Date and Time");

            LayoutInflater inflater = AddCashGame.this.getLayoutInflater();

            dateAndTimePickerDialog.setView(inflater.inflate(R.layout.date_picker_dialog, null));
            dateAndTimePickerDialog.setPositiveButton("Set", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int newYear = datePicker.getYear();
                    int newMonth = datePicker.getMonth() + 1;
                    int newDay = datePicker.getDayOfMonth();
                    int newHour = timePicker.getCurrentHour() % 12;
                    String newMinute = formatter.format(timePicker.getCurrentMinute());
                    String newAMOrPM = timePicker.getCurrentHour() % 12 <= 12 ? " AM" : " PM";

                    String newTime = newMonth + "/" + newDay + "/" + newYear + "   " + newHour + ":" + newMinute + newAMOrPM;

                    startDate.setText(newTime);
                    Toast.makeText(AddCashGame.this, newTime + "",Toast.LENGTH_SHORT).show();
                }
            });
            dateAndTimePickerDialog.setNegativeButton("Cancel", null);
            dateAndTimePickerDialog.show();

        }
    });

Solution

  • This is happening because you're pointing your AlertDialog View to a new one, instead of the one you've inflated in this line:

    final View dialogView = View.inflate(MyActivity.this, R.layout.date_picker_dialog, null);
    

    The first view you've inflated contains both your DatePicker and TimePicker set to the right time, while the new one is set, by default, to Calendar.getInstance().

    Change this line:

    dateAndTimePickerDialog.setView(inflater.inflate(R.layout.date_picker_dialog, null));
    

    To this line:

    dateAndTimePickerDialog.setView(dialogView);