java - Error in Android Edittext Onclick Datepickerdialog in lollipop -
i using datepickerdialog. run on kitkat when run application on lollipop , when click on edit text opens datepickerdialog box when select date give unfortunately stop error. below code datepicker on edittext.
private void setdatetimefield() { fromlabel.setonclicklistener(this); tolabel.setonclicklistener(this); final dateformat dateformat = new simpledateformat("dd-mm-yyyy"); //yyyy/mm/dd hh:mm:ss final date date = new date(); final string u = dateformat.format(date); calendar newcalendar = calendar.getinstance(); fromdatepickerdialog = new datepickerdialog(this, new ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { calendar newdate = calendar.getinstance(); newdate.set(year, monthofyear, dayofmonth); from1 = dateformatter.format(newdate.gettime()); diff1 = newdate.gettimeinmillis(); long d = date.gettime(); if((newdate.gettime()).equals(date)||(newdate.gettime()).after(date)){ long d1 = (diff1 / (24 * 60 * 60 * 1000) - d / (24 * 60 * 60 * 1000)) + 1; if(d1>30){ total.setvisibility(view.visible); total.settext("booking not allowed date given outside advance booking period"); avail.setvisibility(view.gone); } else{ total.setvisibility(view.gone); fromlabel.settext(from1); tolabel.settext(null); to=null; avail.setvisibility(view.gone); from=fromlabel.gettext().tostring(); } } else{ total.setvisibility(view.visible); total.settext("choose date after or equals current date"); fromlabel.settext(""); tolabel.settext(null); from=null; to=null; } } },newcalendar.get(calendar.year), newcalendar.get(calendar.month), newcalendar.get(calendar.day_of_month)); fromdatepickerdialog.setbutton(dialoginterface.button_positive, getstring(r.string.done), new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { if (which == dialoginterface.button_positive) { dialog.cancel(); if(type.equals("according time")) { int cnt=-1; if(from1.equals(u)){ cnt = 1; loadtimespinnerdataatt(text,from,cnt); } else if(total.gettext()=="choose date after or equals current date") { } else if(total.gettext()=="booking not allowed date given outside advance booking period") { } else {cnt = 0; loadtimespinnerdataatt(text,from,cnt); } } } } }); } public void onclick(view view) { if(view == fromlabel) { fromdatepickerdialog.show(); } else if(view == tolabel) { todatepickerdialog.show(); } } public void onclose(dialoginterface dialoginterface) { } }
try it, may you,
date picker error resloved here
import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import android.support.v7.app.actionbaractivity; import android.text.inputtype; import android.app.datepickerdialog; import android.app.dialog; import android.os.bundle; import android.view.view; import android.widget.datepicker; import android.widget.edittext; public class mainactivity extends actionbaractivity { private int year; private int month; private int day; static final int date_picker_id = 1111; // date picker edittext m3_datedisplay; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); m3_datedisplay = (edittext) findviewbyid(r.id.datepick); // current date calender final calendar c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); // show selected date stringbuilder datevalue1 = new stringbuilder().append(day).append("-") .append(month + 1).append("-").append(year).append(" "); // converting correct date format save database simpledateformat sdf123 = new simpledateformat("dd-mm-yyyy"); string abs1 = datevalue1.tostring(); date testdate1 = null; try { testdate1 = sdf123.parse(abs1); } catch (parseexception e) { e.printstacktrace(); } simpledateformat formatter1 = new simpledateformat("dd-mm-yyyy"); string dateformat = formatter1.format(testdate1); m3_datedisplay.settext(dateformat); m3_datedisplay.setfocusable(false); m3_datedisplay.setinputtype(inputtype.type_null); m3_datedisplay.setonclicklistener(new view.onclicklistener() { @suppresswarnings("deprecation") @override public void onclick(view v) { showdialog(date_picker_id); } }); } @override protected dialog oncreatedialog(int id) { switch (id) { case date_picker_id: // open datepicker dialog. // set date picker current date // add pickerlistener listner date picker // return new datepickerdialog(this, pickerlistener, year, month, // day); // ///only show till date not more that. datepickerdialog dialog = new datepickerdialog(this, pickerlistener, year, month, day); dialog.getdatepicker().setmaxdate(new date().gettime()); return dialog; } return null; } private datepickerdialog.ondatesetlistener pickerlistener = new datepickerdialog.ondatesetlistener() { // when dialog box closed, below method called. @override public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) { year = selectedyear; month = selectedmonth; day = selectedday; // show selected date stringbuilder datevalue = new stringbuilder().append(day) .append("-").append(month + 1).append("-").append(year) .append(" "); // converting correct date format save database simpledateformat sdf123 = new simpledateformat("dd-mm-yyyy"); string abs1 = datevalue.tostring(); date testdate1 = null; try { testdate1 = sdf123.parse(abs1); } catch (parseexception e) { e.printstacktrace(); } simpledateformat formatter1 = new simpledateformat("dd-mm-yyyy"); string dateformat = formatter1.format(testdate1); m3_datedisplay.settext(dateformat); } }; } change minimum api-11 in manifest
Comments
Post a Comment