How to set a minimum date in the datapicker?

Asked

Viewed 576 times

2

I would like to put a minimum date on my datapicker, because I cannot yet: follows below the code so you can help me, and in my layout only has an Edit text and the datapicker only appears when and I click on the text box.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    tools:context="br.com.eduspaceandroid.cursoandroid.eduspace.Activity.AgendamentoActivity"
    android:id="@+id/dpDatainicial">
    <include android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
 <EditText
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:inputType="date"
        android:ems="10"
        android:padding="10dp"
        android:id="@+id/etDataInicial"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="@string/hint_Data"
        " />

code of my scheduling Activity:

public class AgendamentoActivity extends AppCompatActivity {
    private Toolbar toolbarAgendamento;
    private EditText datainicial;
    private int ano;
    private int mes;
    private int dia;
    static final int DATE_DIALOG_ID =0;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agendamento);
        toolbarAgendamento=(Toolbar)findViewById(R.id.toolbar);
        toolbarAgendamento.setTitle("Agendamento");
        setSupportActionBar(toolbarAgendamento);

        datainicial=(EditText)findViewById(R.id.etDatainicial);
        String data = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
        datainicial.setText(data);

        datainicial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showDialog(DATE_DIALOG_ID);
            }
        });

    }


    protected Dialog onCreateDialog(int id) {
        switch (id){
            case DATE_DIALOG_ID:

                return new DatePickerDialog(AgendamentoActivity.this,mDateSetListener,
                        dia,mes,ano);

        }
        return null;
    }


    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view,int day, int month, int year ) {

            dia = day;
            mes = month;
            ano = year;

            updateDisplay();
        }

    };

    private void updateDisplay() {
        datainicial.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(dia).append("/").append(mes + 1).append("/")
                .append(ano).append(" "));

    }

1 answer

2

Use the method setMinDate. See an example, if you want to set the minimum date from the current moment, just do it this way below:

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

If you want to limit the minimum date by a specific date, you can use Calendar, for example:

final Calendar calendar = Calendar.getInstance();  
calendar.set(2010, Calendar.OCTOBER, Calendar.DAY_OF_MONTH);
datePicker.setMinDate(calendar.getTimeInMillis()); 
  • 1

    Why - 1000?

  • 2

    Due to the base implementation of the Datepicker class, it is not possible to set the minimum date as exactly now. Ref.: https://stackoverflow.com/questions/13661788/how-to-set-minimum-datepicker-date-to-current-date Anyway, we’re talking about taking a second!

  • Friend added datepicker.setMinDate(System.currentTimeMillis() - 1000). Only there is a problem: when you open the emulator and I click on the text box starts with an old date of the year 1900... only after I click on a random date. and I click again on the text box it appears of the month and current year.

  • how do I now have two dates (one initial and one final) in my app?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.