android - How to set the limit on date in Date picker dialog -
i want put limit on date user can not pick date more that, example if today 1 january user should not able select more 7 dates , mean can not select 9 january. want him not select month , year. putting limit set task in 1 week.
what have done far showing date picker fragment , setting current date in it. code in main activity goes this:
etselectdate.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dialogfragment datepickerfragment = new datepickerfragment() { @override public void ondateset(datepicker view, int year, int month, int day) { log.d("date change", "ondateset"); calendar c = calendar.getinstance(); c.set(year, month, day); dateformat df = dateformat.getdateinstance(); etselectdate.settext(df.format(c.gettime())); //nextfield.requestfocus(); //moves focus else after dialog closed } }; datepickerfragment.show(mainactivity.this.getsupportfragmentmanager(), "datepicker"); } });
and date picker fragment class goes :
public static class datepickerfragment extends dialogfragment implements datepickerdialog.ondatesetlistener{ @override public dialog oncreatedialog(bundle savedinstancestate) { // use current date default date in picker final calendar c = calendar.getinstance(); int year = c.get(calendar.year); int month = c.get(calendar.month); int day = c.get(calendar.day_of_month); // create new instance of datepickerdialog , return return new datepickerdialog(getactivity(), this, year, month, day); } @override public void ondateset(datepicker view, int year, int month, int day) { //blah } }
till working fine , not know how put limit on date , rest of months , year should non select able . have seen many link such this , not understand how can , there nothing helpful on android site.
so please me , how can put limit of 7 days only
update
through replies know how set max date in calender , want set max date 7 days ahead of current date , still not getting it. method read far :
pickerdialog.getdatepicker().setmaxdate(new date().gettime());
it setting current date maximum, how can add 7 days ahead in since date object ? please
you have setmindate(long)
, setmaxdate(long)
methods @ disposal. both of these work on api level 11 , above. since using datepickerdialog
, need first underlying datepicker calling getdatepicker()
method.
dpdialog.getdatepicker().setmindate(mindate); dpdialog.getdatepicker().setmaxdate(maxdate);
source :set limit on datepickerdialog in android?
you can calculate mindate using,
date today = new date(); calendar c = calendar.getinstance(); c.settime(today); c.add( calendar.month, -6 ) // subtract 6 months long mindate = c.gettime().gettime() // twice!
updated :
replace below line
return new datepickerdialog(getactivity(), this, year, month, day);
with
// create new instance of datepickerdialog , return datepickerdialog pickerdialog = new datepickerdialog(getactivity(), this, year, month, day); pickerdialog.getdatepicker().setmaxdate(maxdate); pickerdialog.getdatepicker().setmindate(mindate); return pickerdialog;
Comments
Post a Comment