I have set min date as current date in the datePickerDialog using setMinDate().. Now i want to set the same to the timePickerDialog.. Is that possible ? if yes please help..
public class AddTasks extends Activity {
TaskForm activityForm = new TaskForm();
private Button pPickDate;
private int pYear;
private int pMonth;
private int pDay;
public String date;
public String Loc;
public double lat;
public double lon;
private Button pPickTime;
private int mHour;
private int mMinute;
public String time;
private TimePickerDialog timePickerDialog;
private DatePickerDialog datePickerDialog;
Date selectedDate;
/** This integer will uniquely define the dialog to be used for displaying date picker.*/
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
/** 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;
pDay = dayOfMonth;
updateDate();
displayDateToast();
}
};
private TimePickerDialog.OnTimeSetListener mTimeListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hour, int minute) {
mHour = hour;
mMinute = minute;
updateTime();
displayTimeToast();
}
};
/** Updates the date in the TextView */
private void updateDate() {
/** pDisplayDate.setText( */ date =
new StringBuilder()
// Month is 0 based so add 1
.append(pDay).append("/")
.append(pMonth + 1).append("/")
.append(pYear).append(" ").toString();
// date = pDisplayDate.getText().toString();
}
private void updateTime() {
/** pDisplayTime.setText( */ time =
new StringBuilder()
.append(mHour).append(":")
.append(mMinute).append("").toString();
}
/** Displays a notification when the date is updated */
private void displayDateToast() {
//Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(date), Toast.LENGTH_LONG).show();
activityForm.setTaskDateInput(date);
}
private void displayTimeToast() {
// Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(time), Toast.LENGTH_LONG).show();
activityForm.setTaskTimeInput(time);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_task);
init();
/** 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() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** Capture our View elements */
//pDisplayTime = (TextView) findViewById(R.id.displayTime);
pPickTime = (Button) findViewById(R.id.pickTime);
/** Listener for click event of the button */
pPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
/** Get the current date and time */
final Calendar cal = Calendar.getInstance();
pYear = cal.get(Calendar.YEAR);
pMonth = cal.get(Calendar.MONTH);
pDay = cal.get(Calendar.DAY_OF_MONTH);
mHour = cal.get(Calendar.HOUR_OF_DAY);
mMinute = cal.get(Calendar.MINUTE);
}
/** Create a new dialog for date and time picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
datePickerDialog = new DatePickerDialog(this,
pDateSetListener,
pYear, pMonth, pDay);
Date dt = new Date();
Calendar calendar = new Calendar();
calendar.setTime(dt);
mHour = calendar.get(Calendar.HOUR_OF_DAY);
mMinute = calendar.get(Calendar.MINUTE);
datePickerDialog.getDatePicker().setMinDate(dt.getTime() - 10000);
// datePickerDialog.getDatePicker().setMinDate(new Date().getTime() - 10000);
return datePickerDialog;
case TIME_DIALOG_ID:
timePickerDialog = new TimePickerDialog(this,
mTimeListener,
mHour,mMinute,true) ;
return timePickerDialog;
}
return null;
}
I have Edited my Post by adding the whole code..
I have 2 buttons for datepickerdialog and timepickerdialog each.. what i want is when i click on the button and the dialog opens then the user should not be able to set past date or time in the dialog i.e dialog should show current date and time.. By setMinDate() on Datepickerdialog m able to do for date but for time there's no such method.. how can i do this.. ?
Answers
Try this out, you get the hour and minute from the datePickerDialog Date object when you set it to minimum date,
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
datePickerDialog = new DatePickerDialog(this,
pDateSetListener,
pYear, pMonth, pDay);
Date dt = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dt);
mHour = calendar.get(Calendar.HOUR_OF-DAY);
mMinute = calendar.get(Calendar.MINUTE);
datePickerDialog.getDatePicker().setMinDate(dt.getTime() - 10000);
return datePickerDialog;
case TIME_DIALOG_ID:
timePickerDialog = new TimePickerDialog(this,
mTimeListener,
mHour,mMinute,true) ;
return timePickerDialog;
}
return null;
}