Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Update minDate and maxDate of a DatePickers many times for Start date and End date

I have two Datepickers dialog for ToDate[start] and FromDate[end].

I am setting both datePicker max date is current date by using setMaxDate() function.

My issue is that when

1) I m change the date of ToDatePicker I wont set setMaxDate() of FromDatePicker.

2) I m change the date of FromDatePicker I wont set setMinDate() of ToDatePicker.

In My code this work 1st time but next time when I m changing both or single datepicker it not set setMaxDate(), setMinDate() function

here is my code

dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
    Calendar newCalendar = Calendar.getInstance();

    //Setting first None value to updatedDate To and from
    Util.SaveToSharedPref(MgntNCActivity.this,
            Constant.ShaPreMgntNcFilter,
            Constant.ShaPreMgntNcFilterUpdatedToDate, Constant.NoValue);
    Util.SaveToSharedPref(MgntNCActivity.this,
            Constant.ShaPreMgntNcFilter,
            Constant.ShaPreMgntNcFilterUpdatedFromDate, Constant.NoValue);

    datePickerDiaFrom=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Calendar upDateFrom = Calendar.getInstance();
            upDateFrom.set(year, monthOfYear, dayOfMonth);
            txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));
            // System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());

         //setting minimum limit to date Picker 'To'
           if (dateFormatter.format(upDateFrom.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate()))) {
                upDateFrom.set(Calendar.HOUR_OF_DAY, upDateFrom.getMinimum(Calendar.HOUR_OF_DAY));
                upDateFrom.set(Calendar.MINUTE, upDateFrom.getMinimum(Calendar.MINUTE));
                upDateFrom.set(Calendar.SECOND, upDateFrom.getMinimum(Calendar.SECOND));
                upDateFrom.set(Calendar.MILLISECOND, upDateFrom.getMinimum(Calendar.MILLISECOND));

                datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());

                System.out.println("setting To DatePicker current date as minDate : "
                                            +dateFormatter.format(NetworkUtil.getTodayDate()));
            }else{
                datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
                System.out.println("setting To DatePicker minDate : "
                            +dateFormatter.format(upDateFrom.getTime()));

            }

           //datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());

            Util.SaveToSharedPref(MgntNCActivity.this,
                    Constant.ShaPreMgntNcFilter,
                    Constant.ShaPreMgntNcFilterUpdatedFromDate, dateFormatter.format(upDateFrom.getTime()));
        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    datePickerDiaTo=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            Calendar upDateTo = Calendar.getInstance();
            upDateTo.set(year, monthOfYear, dayOfMonth);
            txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));
            // System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());

         //setting maximum limit to date Picker 'From'
           if (dateFormatter.format(upDateTo.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate()))) {
              upDateTo.set(Calendar.HOUR_OF_DAY, upDateTo.getMaximum(Calendar.HOUR_OF_DAY));
              upDateTo.set(Calendar.MINUTE, upDateTo.getMaximum(Calendar.MINUTE));
              upDateTo.set(Calendar.SECOND, upDateTo.getMaximum(Calendar.SECOND));
              upDateTo.set(Calendar.MILLISECOND, upDateTo.getMaximum(Calendar.MILLISECOND));

              datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());

             System.out.println("setting From DatePicker current date as maxDate : "
                        +dateFormatter.format(NetworkUtil.getTodayDate()));
            }else{
                datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
                System.out.println("setting From DatePicker maxDate : "
                        +dateFormatter.format(upDateTo.getTime()));
            }

           //datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());

            Util.SaveToSharedPref(MgntNCActivity.this,
                    Constant.ShaPreMgntNcFilter,
                    Constant.ShaPreMgntNcFilterUpdatedToDate, dateFormatter.format(upDateTo.getTime()));

        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    //setting max limit to date Picker
    datePickerDiaTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
    datePickerDiaFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());

Any one please suggest me solution of this ... Thanks...

like image 771
SAndroidD Avatar asked Oct 19 '22 18:10

SAndroidD


1 Answers

finally I found solution :)

I m writing datePickerDialogs 'datePickerDiaFrom' , 'datePickerDiaTo' are class variables that's why when I m changing minDate or maxDate at that time it not replace previous date .

My solution is ,

each time when I click on textview to open new dialogPicker and set range to it.

here is my code :

static long MaximumDate=0,MinimunDate=0;

dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

    View.OnClickListener showDatePicker = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final View vv = v;

            Calendar newCalendar=Calendar.getInstance();    

            switch (vv.getId()) {
            case R.id.txtFromUpdatedDate_MgntNC:

                DatePickerDialog dialogFrom=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Calendar upDateFrom = Calendar.getInstance();
                        upDateFrom.set(year, monthOfYear, dayOfMonth);
                        txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));

                        MinimunDate=upDateFrom.getTimeInMillis();


                    }

                }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


                if (MaximumDate!=0) {
                    dialogFrom.getDatePicker().setMaxDate(MaximumDate);
                }else{
                    dialogFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
                }

                dialogFrom.show();

                dialogFrom.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        MinimunDate=0;

                         txtFromUpdatedDate.setText(Constant.NoValue);
                    }
                });

                break;

            case R.id.txtToUpdatedDate_MgntNC:
                DatePickerDialog dialogTo=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Calendar upDateTo = Calendar.getInstance();
                        upDateTo.set(year, monthOfYear, dayOfMonth);
                        txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));

                        MaximumDate=upDateTo.getTimeInMillis();

                    }

                }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

                dialogTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
                if (MinimunDate!=0) {
                    dialogTo.getDatePicker().setMinDate(MinimunDate);
                }else{
                    //dialog1.getDatePicker().setMinDate(NetworkUtil.getTodayDate().getTime());
                }

                dialogTo.show();

                dialogTo.setOnCancelListener(new DialogInterface.OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface arg0) {
                        MaximumDate=0;

                    }
                });


                break;  

            default:
                break;
            }



        }
    };

    txtFromUpdatedDate.setOnClickListener(showDatePicker);
    txtToUpdatedDate.setOnClickListener(showDatePicker);
like image 54
SAndroidD Avatar answered Oct 22 '22 08:10

SAndroidD