Search code examples
androidandroid-datepicker

Android datepicker disable all dates prior to 1st dec 2014


i have successfully implemented datepicker in android my problem is i want to disable all dates in datepicker prior to 1 st dec 2014 how can that be done ,i tried looking for examples but they all provide examples to disable date prior to todays date

i have not implemented any code for disabling previous dates any working example would be good thanks in advance

here is my code

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

     /** Private members of the class */
    private TextView pDisplayDate;
    private Button pPickDate;
    private int pYear;
    private int pMonth;
    private int pDay;
    /** This integer will uniquely define the dialog to be used for displaying date picker.*/
    static final int DATE_DIALOG_ID = 0;

    /** Callback received when the user "picks" a date in the dialog */
    private DatePickerDialog.OnDateSetListener pDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    pYear = year;
                    pMonth = monthOfYear + 1;
                    pDay = dayOfMonth;

//                    if (pMonth<10)
//                         pMonth='0'+pMonth;
//                       if (pDay<10)
//                         pDay='0'+pDay;


                    updateDisplay();
                    displayToast();
                }
            };

    /** Updates the date in the Te
     * xtView */
    private void updateDisplay() {

        String formattedMonth = "" + pMonth;
        String formattedDayOfMonth = "" + pDay;

        if(pMonth < 10){

            formattedMonth = "0" + pMonth;
        }
        if(pDay < 10){

            formattedDayOfMonth = "0" + pDay;
        }
       // searchText.setText(formattedDayOfMonth + "/" + formattedMonth + "/" + year);


        pDisplayDate.setText(
            new StringBuilder()
//                    // Month is 0 based so add 1
                    .append(pYear).append("-")
                    .append(formattedMonth).append("-")
                    .append(formattedDayOfMonth).append(" "));
    }

    /** Displays a notification when the date is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(pDisplayDate.getText()),  Toast.LENGTH_SHORT).show();

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** Capture our View elements */
        pDisplayDate = (TextView) findViewById(R.id.displayDate);
        pPickDate = (Button) findViewById(R.id.pickDate);

        /** Listener for click event of the button */
        pPickDate.setOnClickListener(new View.OnClickListener() {
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        /** Get the current date */
        final Calendar cal = Calendar.getInstance();
        pYear = cal.get(Calendar.YEAR);
        pMonth = cal.get(Calendar.MONTH);
        pDay = cal.get(Calendar.DAY_OF_MONTH);

        /** Display the current date in the TextView */
        updateDisplay();
    }

    /** Create a new dialog for date picker */
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, 
                        pDateSetListener,
                        pYear, pMonth, pDay);
        }
        return null;
    }
} 

Solution

  • set Max date like this :

    protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:
    DatePickerDialog dialog = new DatePickerDialog(mContext,
                            datePickerListener, myCalendar.get(Calendar.YEAR),
                            myCalendar.get(Calendar.MONTH), myCalendar
                                    .get(Calendar.DAY_OF_MONTH));
                 dialog.getDatePicker().setMaxDate(new Date().getTime());
                        dialog.show();
            }
            return null;
        }