Reputation: 139
I had a requirement for making a dynamic time picker dialog in android. Basically the time changes every time I pick a date from calendar and the range has to change each time. Thus, it means suppose in date 4/5/2017, the time range is from 9-2 the minimum value has to be 9 and maximum has to be 2 with a increment of 1. Till now what I have done is just set a time picker dialog
private void calltimerange() {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
final TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
// Toast.makeText(context, "" + selectedHour + ":" + selectedMinute, Toast.LENGTH_SHORT).show();
tvTime.setText(String.valueOf(selectedHour));
mytime = String.valueOf(selectedHour);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
Any help would be appreciated. Thank You
Upvotes: 0
Views: 1174
Reputation: 872
private void editTextDatePicker() {
final EditText et_datepicker = new EditText(this);
final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
et_datepicker.setLayoutParams(lparams);
et_datepicker.setPadding(20, 10, 20, 10);
et_datepicker.setHint("Pick your Date");
et_datepicker.setFocusable(false);
et_datepicker.setGravity(1);
et_datepicker.setBackgroundResource(R.drawable.edittext_background);
Calendar newCalendar = Calendar.getInstance();
final DatePickerDialog datepicker = new DatePickerDialog(this, R.style.dialogTheme, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
et_datepicker.setText(dateFormatter.format(newDate.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
et_datepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datepicker.show();
}
});
linerLayoutChild.addView(et_datepicker); // here lineaLayoutChild is the object for your layout where you want to add.
}
Upvotes: 0
Reputation: 427
I suggest you to use if you want in easy way :
MaterialDateTimePicker by wdullaer
and look for this feature:
setMinTime(Timepoint time)
Set the minimum valid time to be selected. Time values earlier in the day will be deactivated
setMaxTime(Timepoint time)
Set the maximum valid time to be selected. Time values later in the day will be deactivated
setSelectableTimes(Timepoint[] times)
You can pass in an array of Timepoints. These values are the only valid selections in the picker. setMinTime(Timepoint time) and setMaxTime(Timepoint time) will further trim this list down.
Upvotes: 1