Search code examples
javaandroiddatedaterangepicker

Android Studio - Show selected dates from dateRangePicker on TextView


Does anyone know how to show selected dates from dateRangePicker on TextView? This function is supposed to show the selected date range once user has picked the date from the dialog.

Here is my code

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_cuti);

    btnDate = findViewById(R.id.btnDate);
    pilihDate = findViewById(R.id.mvPilihTarikh);
    
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.clear();

    Pair<Long, Long> selectionDates=null;

    Long today = MaterialDatePicker.todayInUtcMilliseconds();

    calendar.setTimeInMillis(today);

    calendar.roll(Calendar.MONTH, Calendar.JANUARY);
    Long january = calendar.getTimeInMillis();

    calendar.roll(Calendar.MONTH, Calendar.DECEMBER);
    Long december = calendar.getTimeInMillis();

    CalendarConstraints.Builder constraintBuilder = new CalendarConstraints.Builder();
    constraintBuilder.setValidator(DateValidatorPointForward.now());

    MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
    CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
    builder.setCalendarConstraints(constraintsBuilder.build());
    MaterialDatePicker<?> picker = builder.build();
    picker.show(getSupportFragmentManager(), builder.toString());
    builder.setTitleText("PILIH TARIKH");
    builder.setSelection(selectionDates);
    builder.setCalendarConstraints(constraintBuilder.build());
    MaterialDatePicker materialDatePicker = builder.build();

    btnDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            materialDatePicker.show(getSupportFragmentManager(), materialDatePicker.toString());

        }
    });

}

Solution

  • You can check this tutorial. It uses material date picker.

    https://www.geeksforgeeks.org/material-design-date-picker-in-android/

    final MaterialDatePicker materialDatePicker = materialDateBuilder.build();
    
        // handle select date button which opens the
        // material design date picker
        mPickDateButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // getSupportFragmentManager() to
                        // interact with the fragments
                        // associated with the material design
                        // date picker tag is to get any error
                        // in logcat
                        materialDatePicker.show(getSupportFragmentManager(), "MATERIAL_DATE_PICKER");
                    }
                });
    
        // now handle the positive button click from the
        // material design date picker
        materialDatePicker.addOnPositiveButtonClickListener(
                new MaterialPickerOnPositiveButtonClickListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onPositiveButtonClick(Object selection) {
    
                        // if the user clicks on the positive
                        // button that is ok button update the
                        // selected date
                        mShowSelectedDateText.setText("Selected Date is : " + materialDatePicker.getHeaderText());
                        // in the above statement, getHeaderText
                        // is the selected date preview from the
                        // dialog
                    }
                });