How to set a minimum date in the datapicker?

2

I would like to put a minimum date in my datapicker, because I can not do it yet: Here is the code for you to help me. In my layout, there is only one edit text and the datapicker only appears when 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"
        " />

my schedule activity code:

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(" "));

    }
    
asked by anonymous 08.09.2017 / 12:11

1 answer

2

Use the setMinDate method. Here's an example, if you want to set the minimum date from the current time, just do it this way:

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()); 
    
08.09.2017 / 12:30