How to restrict minimum and maximum values of a DatePickerDialog that emerges from an EditText of Date type in Android?

1

I want to set the minimum and maximum value of a DatePickerDialog that emerges when I click on an Edit Text of type Date, according to what I have researched, it can be done with this:

setMaxDate(long maxDate)
setMinDate(long minDate)

But I do not 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();
        }
    }
}
    
asked by El Cóndor 13.06.2016 в 01:26
source

2 answers

1

You must use setMinDate (long maxDate)

datePicker.setMinDate(System.currentTimeMillis() - 1000); 

and setMaxDate (long minDate)

Calendar c = Calendar.getInstance();
c.set(2016, 6, 18);
datePicker.setMaxDate(c.getTimeInMillis());

update:

I remembered that there was a similar question here How to disable days in the android datepicker? , this would be an example:

We define variables to configure our DatePickerDialog:

   private int miAnio, miMes, miDia;

This is an example to create a DatePicker and define a minimum date, for example we define it to be the previous month and a day before:

  Calendar calendar =  Calendar.getInstance();
    miAnio = calendar.get(Calendar.YEAR);
    miMes = calendar.get(Calendar.MONTH);
    miDia = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog oyenteSelectorFecha = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            Log.i("TAG", String.valueOf(year) + String.valueOf(monthOfYear) + String.valueOf(dayOfMonth));

        }
    }, miAnio, miMes, miDia);


    //Como ejemplo: deseamos que la fecha minima sea un mes antes y un dia antes.
    Calendar calendarioMin = Calendar.getInstance();
    calendarioMin.add(Calendar.MONTH, - 1); //Mes anterior
    calendarioMin.add(Calendar.DAY_OF_MONTH, - 1); //dia anterior
    //defines que el día que deseas
    oyenteSelectorFecha.getDatePicker().setMinDate(calendarioMin.getTimeInMillis() - 1000);
    oyenteSelectorFecha.show();

Therefore your DatePicker will only allow at least the value defined in calendarioMin.getTimeInMillis() .

    
answered by 13.06.2016 / 16:49
source
1

You must get the DatePicker and set the limits to it.

DatePickerDialog dateDialod = new DatePickerDialog(this, oyenteSelectorFecha, anio, mes, dia);
dateDialod.getDatePicker().setMinDate(<aquí van los milisegundos de la fecha>);
    
answered by 13.06.2016 в 16:22