I want to set the minimum and maximum value of a DatePickerDialog
that emerges when clicking on a EditText
Date type, according to what I have investigated it can be done with this:
setMaxDate(long maxDate)
setMinDate(long minDate)
But I don't know how to apply it to the code I have, my code is as follows:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static EditText fechauso, horainicio;
private int anio, dia, mes, hora, minuto;
private static final int TIPO_DIALOGO = 0;
private static DatePickerDialog.OnDateSetListener oyenteSelectorFecha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fechauso = (EditText) findViewById(R.id.txt_fechadeuso);
horainicio = (EditText) findViewById(R.id.txt_horainicio);
horainicio.setOnClickListener(this);
fechauso.setInputType(InputType.TYPE_NULL);
Calendar calendario = Calendar.getInstance();
anio = calendario.get(Calendar.YEAR);
mes = calendario.get(Calendar.MONTH);
dia = calendario.get(Calendar.DAY_OF_MONTH);
oyenteSelectorFecha = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
anio = year;
mes = monthOfYear;
dia = dayOfMonth;
mostrarFecha();
horainicio.requestFocus();
}
};
fechauso.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
closeSoftKeyBoard();
mostrarCalendario(fechauso);
}
}
});
horainicio.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
closeSoftKeyBoard();
mostrarTime();
}
}
});
}
public void mostrarCalendario(View control) {
showDialog(TIPO_DIALOGO);
}
public void mostrarFecha() {
fechauso.setText(anio + "-" + (mes + 1) + "-" + dia);
}
public void mostrarTime() {
// Get Current Time
final Calendar c = Calendar.getInstance();
hora = c.get(Calendar.HOUR);
minuto = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
horainicio.setText(hourOfDay + ":" + minute + ":00");
}
}, hora, minuto, false);
timePickerDialog.show();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new DatePickerDialog(this, oyenteSelectorFecha, anio, mes, dia);
}
return null;
}
public void closeSoftKeyBoard() {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
@Override
public void onClick(View v) {
if (v == horainicio) {
closeSoftKeyBoard();
mostrarTime();
}
}
}
You must obtain
DatePicker
him and him to set the limits.You must use setMinDate(long maxDate)
and setMaxDate(long minDate)
update:
I remembered a similar question existed here How to disable days in android datepicker? , this would be an example:
We define variables to configure our DatePickerDialog:
This is an example to create a DatePicker and define a minimum date, for example we define it to be the previous month and one day before:
Therefore, your DatePicker will only allow at least the value defined in
calendarioMin.getTimeInMillis()
.