0

I want to develop a reservation application in Android. For the date field, I'm using the datePickerDialog. However, I want to disable the decrementing button when I reach the current date so the user can't make a reservation in the past.

Do you have any idea how to do that?

This is my activity class:

public class DatePickerActivity extends Activity {

private int mYear;
private int mMonth;
private int mDay;


private TextView mDateDisplay;
private Button mPickDate;

static final int DATE_DIALOG_ID = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mDateDisplay = (TextView) findViewById(R.id.showMyDate);        
    mPickDate = (Button) findViewById(R.id.myDatePickerButton);

    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });


    // get the current date
    final Calendar c = Calendar.getInstance();

    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);


    // display the current date
    updateDisplay();

}
private void updateDisplay() {

    String selectedDate = mDay+"-"+ mMonth + 1 +  "-"+mYear;

    this.mDateDisplay.setText(
        new StringBuilder()
                .append(mDay).append("-")
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")

                .append(mYear).append(" "));
}

private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };

        @Override
        protected Dialog onCreateDialog(int id) {
           switch (id) {
           case DATE_DIALOG_ID:
              return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);

           }

           return null;
        }
}
Renjith
  • 5,783
  • 9
  • 31
  • 42
Ðina YŏŜri
  • 55
  • 2
  • 10

1 Answers1

0

Use your Calendar object to compare the date value and set the value in calendar on date change

Calendar c;

oncreate(){
// initialize Calendar object
}

private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {

            Date date = new Date();
            c = Calendar.getIntance(); // here you need to create new instance of calendar because its time was older
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            c.set(mYear, mMonth, mDay);

            // now check here your Calendar date was after current date then update
            // otherwise do nothing

            if(c.getTime().getTime()>date.getTime()){ // updated condition
                updateDisplay();
            }
        }
    };
Pratik
  • 30,639
  • 18
  • 84
  • 159