spykeburn
spykeburn

Reputation: 163

Display datepicker dialog from current month and date android

I want to display date picker dialog from current Month only not Year. I have used setMin to current date which works fine but still I am able to go back till Jan 2015 even though it is not selectable. How do I make it work?

public void show() {
    Calendar now = Calendar.getInstance();

     DatePickerDialog dpd = DatePickerDialog.newInstance(
        Post.this,
        now.get(Calendar.YEAR),
        now.get(Calendar.MONTH),
        now.get(Calendar.DAY_OF_MONTH)
    );

    dpd.setMinDate(now.getInstance());
    now.add(Calendar.DAY_OF_MONTH, 30);

    dpd.setMaxDate(now);

    dpd.show(getFragmentManager(), "Datepickerdialog");
}

Upvotes: 2

Views: 1963

Answers (2)

Harish
Harish

Reputation: 3117

Try below it'll not show you the past dates.It'll show from current date.

  final Calendar calendar = Calendar.getInstance();
  int year = calendar.get(Calendar.YEAR);
  int month = calendar.get(Calendar.MONTH);
  int day = calendar.get(Calendar.DAY_OF_MONTH);
  DatePickerDialog dpd = new DatePickerDialog(this,
                         AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
  dpd.show();
  calendar.add(Calendar.DATE, 0);
  // Set the Calendar new date as minimum date of date picker
  dpd.getDatePicker().setMinDate(calendar.getTimeInMillis());
                         (or)
 dpd.setMinDate(System.currentTimeMillis() - 1000); 

Upvotes: 1

johnrao07
johnrao07

Reputation: 6908

Here you go, This does the work.

DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
dueDateCalendar.get(Calendar.YEAR), 
dueDateCalendar.get(Calendar.MONTH), 
dueDateCalendar.get(Calendar.DAY_OF_MONTH))
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    int year = getContext().getResources()
        .getIdentifier("android:id/year", null, null);
    if(year != 0){
        View yearPicker = findViewById(year);
        if(yearPicker != null){
            yearPicker.setVisibility(View.GONE);
        }
    }
}
};
return dlg;

Upvotes: 0

Related Questions