Android Studio : How to get date from datepicker and time picker and display on the screen? -
android studio : how date datepicker , time picker , display on screen permanent text(not toast)?
public class dialoghandlerdate extends dialogfragment { @nonnull @override public dialog oncreatedialog(bundle savedinstancestate) { datesettings datesettings = new datesettings(getactivity()); 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 dialog; dialog = new datepickerdialog(getactivity(),datesettings,year,month,day); return dialog; //return super.oncreatedialog(savedinstancestate); } }
date settings activity:
public class datesettings implements datepickerdialog.ondatesetlistener { context context; public datesettings(context context){ this.context=context; } @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { toast.maketext(context,"selected date:"+monthofyear+" / "+dayofmonth+" / "+year,toast.length_long).show(); charsequence text = ""+dayofmonth+"-"+""+monthofyear+"-"+"" +year; int duration=toast.length_short; toast toast= toast.maketext(context,text,duration); toast.show(); } }
here code, can implements on mainactivity , put fragment on onclicklistener corresponding set date / time:
private textview mdatedisplay; private int myear; private int mmonth; private int mday; // capture ui elements mdatedisplay = (textview) findviewbyid(r.id.datedisplay); // put on onclicklistener "change date" // create new datepickerfragment dialogfragment newfragment = new datepickerfragment(); // display datepickerfragment newfragment.show(getfragmentmanager(), "datepicker"); // callback called when user sets date public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { myear = year; mmonth = monthofyear; mday = dayofmonth; updatedate(); } // update date string in textview private void updatedate() { mdatedisplay.settext(new stringbuilder() // month 0 based add 1 .append(mmonth + 1).append("-").append(mday).append("-") .append(myear).append(" ")); } public static class datepickerfragment extends dialogfragment implements ondatesetlistener { @override public dialog oncreatedialog(bundle savedinstancestate) { // set current date in datepickerfragment 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); } // callback datepickeractivity.ondateset() update ui @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { ((ondatesetlistener) getactivity()).ondateset(view, year, monthofyear, dayofmonth); } }
the same goes set time too:
private textview mtimedisplay; private int mhour; private int mminute; // capture ui elements mtimedisplay = (textview) findviewbyid(r.id.timedisplay); // put on onclicklistener "change time" // create new timepickerfragment dialogfragment newfragment = new timepickerfragment(); // display datepickerfragment newfragment.show(getfragmentmanager(), "timepicker"); // callback called when user sets time public void ontimeset(timepicker view, int hourofday, int minute) { mhour = hourofday; mminute = minute; updatetime(); } // update time string in textview private void updatetime() { mtimedisplay.settext(new stringbuilder().append(pad(mhour)).append(":") .append(pad(mminute))); } // prepends "0" 1-digit minutes private static string pad(int c) { if (c >= 10){ return string.valueof(c); } else{ return"0"+string.valueof(c); } } public static class timepickerfragment extends dialogfragment implements ontimesetlistener { @override public dialog oncreatedialog(bundle savedinstancestate) { final calendar c = calendar.getinstance(); int hourofday = c.get(calendar.hour_of_day); int minute = c.get(calendar.minute); // create new instance of timepickerdialog , return return new timepickerdialog(getactivity(), this, hourofday, minute, false); } // callback timepickerfragmentactivity.ontimeset() update ui @override public void ontimeset(timepicker view, int hourofday, int minute) { ((ontimesetlistener) getactivity()).ontimeset(view, hourofday, minute); } }
you can further resource link: datepicker | android developers
Comments
Post a Comment