0

I have 2 datepicker dialogs in my fragment. The date picker activates when clicking on buttons. I have 2 buttons 1 for setting depart date and the other for setting the return date. clicking on the depart button pops one datepicker dialog and clicking on the return button pops out second datepicker dialog. In the depart date picker dialog, I need to disable the dates when the user tries to select a date which is less than todays date. In the return date picker dialog, there two conditons has to be checked. First one, if a user has not selected a depart date from depart date picker dialog, the return date picker dialog should show the date as current date in it. And when a user tries to select a date which is less than todays date, I need to disable the user from doing so. Secondly, if a user has set the depart date using the depart date picker dialog, then the return date picker dialog,should show the date same as the depart date. And when the user tries to select a date below the depart date, he should be tried to stop it. I have implemented all these successfully in my app for ginger bread and below versions. But the problem is that, when I try to implement this from versions above honeycomb, the app is force closing. How can I set the minimum date in the date picker dialog for versions above honeycomb. Also my app is made to work over all versions from froyo to kitkat and so I have set the min sdk version as that of froyo. I need a code which should work on both versions which works on above and below honeycomb. Please help me out. This is my code.

 private int year;
    private int month;
    private int day;
    Boolean greaterthanhoneycomb=false;
    int departureday,departuremonth,departureyear;
    DatePickerDialog.OnDateSetListener datePickerListener,datepicker;


final Button departuredate=(Button)rootView.findViewById(R.id.button1);
    departuredate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            final Calendar c = Calendar.getInstance();
            year  = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day   = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 
                    year, month,day){



                        @Override
                        public void onDateChanged(DatePicker view,
                                int year, int month, int day) {
                            // TODO Auto-generated method stub

                            final Calendar c = Calendar.getInstance();
                           int mYear = c.get(Calendar.YEAR);
                           int mMonth = c.get(Calendar.MONTH);
                          int  mDay = c.get(Calendar.DAY_OF_MONTH);
                            //* this validation allow user to enter date less then current date *
                            if (year < mYear)
                                view.updateDate(mYear, mMonth, mDay);


                        if (month < mMonth && year == mYear)
                            view.updateDate(mYear, mMonth, mDay);

                        if (day < mDay && year == mYear && month == mMonth)
                            view.updateDate(mYear, mMonth, mDay);
                        super.onDateChanged(view, year, month, day);
                        }


            };

dialog.show();


        }
    });
    final Button returndate=(Button)rootView.findViewById(R.id.button2);
    returndate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(departureday==0)
            {
                 final Calendar c = Calendar.getInstance();
                    year  = c.get(Calendar.YEAR);
                    month = c.get(Calendar.MONTH);
                    day   = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog = new DatePickerDialog(getActivity(), datepicker, year, month, day){

                    @Override
                    public void onDateChanged(DatePicker view, int year,
                            int month, int day) {
                        // TODO Auto-generated method stub
                        final Calendar c = Calendar.getInstance();
                           int mYear = c.get(Calendar.YEAR);
                           int mMonth = c.get(Calendar.MONTH);
                          int  mDay = c.get(Calendar.DAY_OF_MONTH);
                          //* this validation allow user to enter date less then current date *
                            if (year < mYear)
                                view.updateDate(mYear, mMonth, mDay);


                        if (month < mMonth && year == mYear)
                            view.updateDate(mYear, mMonth, mDay);

                        if (day < mDay && year == mYear && month == mMonth)
                            view.updateDate(mYear, mMonth, mDay);
                        super.onDateChanged(view, year, month, day);
                    }

                };
                dialog.show();
            }
            else
            {
            DatePickerDialog dialog = new DatePickerDialog(getActivity(), datepicker, departureyear, departuremonth, departureday){

                @Override
                public void onDateChanged(DatePicker view, int year,
                        int month, int day) {
                    // TODO Auto-generated method stub

                       int mYear =departureyear;
                       int mMonth = departuremonth;
                      int  mDay = departureday;
                        //* this validation allow user to enter date less then current date *
                        if (year < mYear)
                            view.updateDate(mYear, mMonth, mDay);


                    if (month < mMonth && year == mYear)
                        view.updateDate(mYear, mMonth, mDay);

                    if (day < mDay && year == mYear && month == mMonth)
                        view.updateDate(mYear, mMonth, mDay);
                    super.onDateChanged(view, year, month, day);
                }

            };
            dialog.show();
            }
        }
    });

    datepicker=new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            String date=dayOfMonth+"-"+(monthOfYear+1)+"-"+year;
            returndate.setText(" "+date);
        }
    };
    datePickerListener 
    = new DatePickerDialog.OnDateSetListener() {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
    int selectedMonth, int selectedDay) {
    //Do whatever you want
    departureday=selectedDay;
    departuremonth=selectedMonth;
    departureyear=selectedYear;
    String date=selectedDay+"-"+(selectedMonth+1)+"-"+selectedYear;
    departuredate.setText(" "+date);
}

};
njnjnj
  • 978
  • 4
  • 23
  • 58

0 Answers0