0

Main Activity

 {
        date = findViewById(R.id.add_date);
        dueDate = findViewById(R.id.add_dueDate);
        final Calendar calendar = Calendar.getInstance();
        date.setOnClickListener(v -> {
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(addScreen.this, (view, year, month, dayOfMonth) -> date.setText(SimpleDateFormat.getDateInstance().format(calendar.getTime())), year, month, day);
            datePickerDialog.show();
        });
        dueDate.setOnClickListener(v -> {
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(addScreen.this, (view, year, month, dayOfMonth) -> dueDate.setText(SimpleDateFormat.getDateInstance().format(calendar.getTime())), year, month, day);
            datePickerDialog.show();
        });
    }

There is something wrong with this code which I am not able to pick. I am getting the Calender (onClick prompt) to select date but even though I'm selecting dates from past, present or future, it's always returning current(today's) date in the Edit Text.

1 Answers1

0

your using the calendar instance without updating it with the selected date, so it contains the current date. you need to set the selected date to the calendar instance before using it.

date.setOnClickListener(v -> {
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(addScreen.this, { view, year, monthOfYear, dayOfMonth -> 
                calendar.set(Calendar.YEAR, year)
                calendar.set(Calendar.MONTH, monthOfYear)
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)

                date.setText(SimpleDateFormat.getDateInstance().format(calendar.getTime()))

                }, year, month, day);
            datePickerDialog.show();
        });

related : Android Datepicker

medyas
  • 1,166
  • 13
  • 21
  • it says "Expected 1 argument but found 2" – Suryansh Kushwaha Nov 18 '20 at 09:51
  • //Calender prompt for Date date = findViewById(R.id.add_date); dueDate = findViewById(R.id.add_dueDate); final Calendar calendar = Calendar.getInstance(); date.setOnClickListener(v -> { year = calendar.get(Calendar.YEAR, year); month = calendar.get(Calendar.MONTH, month); day = calendar.get(Calendar.DAY_OF_MONTH, day); }); – Suryansh Kushwaha Nov 18 '20 at 09:55
  • Can you help me with this issue too? https://stackoverflow.com/questions/64888676/how-to-save-data-in-an-app-to-phone-storage-and-pull-back-that-data-in-app-of – Suryansh Kushwaha Nov 18 '20 at 10:53