1

Hello Guys I am new to android and facing problem I have searched a lot but not getting solution m using DatePickerDialog box and when I am running the application it is selecting previous dates also though I have set minimum date. I have tried by subtracting 1 second also from current time. and DatePicker is Starting from January1990. I want it to start it from today and block previous dates. Thanks ...

public void OnButtonClickDate()

{
btn_date = (Button)findViewById(R.id.button);

edit_date = (EditText)findViewById(R.id.editText);

btn_date.setOnClickListener
(

 new View.OnClickListener() 
 {
    @Override
    public void onClick(View view) 
    {
     DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() 
     {
       @Override
       public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)                                                                             
       view.setMinDate(System.currentTimeMillis());
       edit_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
      }
    }, mYear, mMonth, mDay);
     datePickerDialog.show();
  }
}
);
}
  • what is going on with your formatting? this is a struggle to read :( – apelsoczi Aug 08 '16 at 14:16
  • I am sorry for the mess ... Do you want me to send it in comments by formatting it ? – Soham Kapoor Aug 08 '16 at 14:19
  • You should edit your previous post with formatted code - this will increase your mileage for how you are supported. In a blink of an eye, a person could navigate away from the page who may have your answer. – apelsoczi Aug 08 '16 at 14:20

2 Answers2

0

Try adjusting your code to this.

btn_date = (Button) findViewById(R.id.button);
edit_date = (EditText) findViewById(R.id.editText);
DatePickerDialog datePickerDialog = new
        DatePickerDialog(MainActivity.this,
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {

                datePickerDialog.setMinDate(System.currentTimeMillis());
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                edit_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
            }
        }, mYear, mMonth, mDay);
datePickerDialog.setMinDate(System.currentTimeMillis());
btn_date.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePickerDialog.show();
            }
        }
);

Android Calendar view is a cascade of bugs. If the above method fails you can try removing the CalendarView from your picker. Other methods to bypass these bugs are described here

datePicker.setCalendarViewShown(false);

Alternatively try using a library for this functionality

    compile "com.wdullaer:materialdatetimepicker:2.4.0"
Community
  • 1
  • 1
MrJM
  • 1,214
  • 1
  • 12
  • 26
0

Hey you can use folowing code sample to set the muinimum date in your application:

 Date date = new Date();

    final long todayInMillis = date.getTime();
    final long thirteenyearMillis = 409968000000L;   
    final long eightyYearMillis = 2522880000000L;
    final long maxDate = todayInMillis - thirteenyearMillis;
    final long minDate = todayInMillis - eightyYearMillis;

Hope you will get an idea from above sample...

Riten
  • 2,879
  • 3
  • 24
  • 28