How to set a minimum and maximum limit for a date picker in android? -
i'm working on app in i'm using custom date , time picker. i've created dialog box , inside i'm showing date , time picker respectively want set minimum , maximum limit date picker. such 1 should not able select previous date today , not more month ahead. code
alertdialog.builder builder = new alertdialog.builder(getactivity()); final datepicker picker = new datepicker(getactivity()); try { field f[] = picker.getclass().getdeclaredfields(); (field field : f) { if (field.getname().equals("myearpicker") || field.getname().equals("myearspinner")) { field.setaccessible(true); object yearpicker = new object(); yearpicker = field.get(picker); ((view) yearpicker).setvisibility(view.gone); } } } catch (exception ex) { ex.printstacktrace(); } picker.setcalendarviewshown(false); picker.setmindate(new date().gettime()); builder.settitle("please select date on leaving :") .setview(picker) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { simpledateformat parseformat = new simpledateformat("eee dd mmm"); date date1 = new date(); date1.setdate(picker.getdayofmonth()); date1.setmonth(picker.getmonth()); final string s = parseformat.format(date1); log.e("date", s); simpledateformat sdf=new simpledateformat("mm-dd-yyyy"); date date2=new date(); date2.setdate(picker.getdayofmonth()); date2.setmonth(picker.getmonth()); final string s2=sdf.format(date2); //time picker alertdialog.builder builder = new alertdialog.builder(getactivity()); final timepicker picker = new timepicker(getactivity()); picker.setis24hourview(true); builder.settitle("please select time @ leaving :") .setview(picker) .setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { int hour = picker.getcurrenthour(); int minute = picker.getcurrentminute(); leaving.settextsize(14); leaving.settext(s + " " + hour + ":" + minute); intime=s2+" "+hour+":"+minute; log.e("intime ",intime); } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { leaving.settext(""); intime=""; dialog.dismiss(); } }).create().show(); //time picker } } ) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { leaving.settext(""); intime=""; dialog.dismiss(); } } ) .create().show();
please help. thank you
try this, shows today next 5 year date in datepicker dialog.
public class mainactivity extends activity { textview txtdatepicker; calendar cal,cal1; long maxdate; date date; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); cal = calendar.getinstance(); cal1 = calendar.getinstance(); txtdatepicker = (textview) findviewbyid(r.id.txtdatepicker); txtdatepicker.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub datepickerdialog dialog = new datepickerdialog(mainactivity.this, new ondatesetlistener() { @override public void ondateset(datepicker arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub toast.maketext(mainactivity.this, ""+arg1+"/"+(arg2+1)+"/"+arg3, toast.length_short).show(); } }, cal.get(calendar.year), cal.get(calendar.month), cal.get(calendar.day_of_month)); dialog.getdatepicker().setmindate(system.currenttimemillis()); cal1.add(calendar.year, 5); dialog.getdatepicker().setmaxdate(cal1.gettimeinmillis()); dialog.show(); } }); } }
Comments
Post a Comment