Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable dates before today date in DatePickerDialog Android?

I want to disable the dates before today date in the DatePickerDialog .I am new in android please suggest me how could i do this .Here is my code that i have written for DatePickerDialog

 final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                    myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH, monthOfYear);
                    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    updateLabel(val);
            }
        };
        depart.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                    val=1;
                }
            });

        returnDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                            val=2;
                }
            });


private void updateLabel(int val) {

        String myFormat = "dd/MM/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        Log.d("Date vlue ", "==="+sdf.format(myCalendar.getTime()));
        if(val==1)
            depart.setText(sdf.format(myCalendar.getTime()));
        else
            returnDate.setText(sdf.format(myCalendar.getTime()));

        }

Please suggest me what have to do

like image 629
Developer Avatar asked Jul 26 '13 10:07

Developer


1 Answers

// Create a new instance of DatePickerDialog and return it

 DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this,year,month,day);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
            return datePickerDialog;
like image 70
Dhara Patel Avatar answered Oct 12 '22 22:10

Dhara Patel