android - How to remove OnClickListener method & directly show the result in TextView -
i have build application in i'm getting days difference result in textview
when click on textview
. have done searching on google. want result should automatically should displayed in textview
. have given id textview
in want show result automatically, id no_of_days.
my code is:
public class leave extends appcompatactivity { textview date; private datepickerdialog datepickerdialog; //textview date2; //textview setday; private datepickerdialog datepickerdialog2; textview no_of_days; string date1 = "", date2 = ""; private textview strdate2; private textview strdate; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_leave); strdate = (textview) findviewbyid(r.id.date); strdate2 = (textview) findviewbyid(r.id.date2); //setday = (textview) findviewbyid(r.id.setday); no_of_days = (textview) findviewbyid(r.id.no_of_days); final radiobutton radio_full = (radiobutton) findviewbyid(r.id.radio_full); final radiobutton radio_half = (radiobutton) findviewbyid(r.id.radio_half); radiogroup radiogroup = (radiogroup) findviewbyid(r.id.radioleave); radiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup radiogroup, int i) { int buttonid = radiogroup.getcheckedradiobuttonid(); switch (buttonid) { case r.id.radio_full: toast.maketext(getapplicationcontext(), "you have selected full day leave", toast.length_short).show(); break; case r.id.radio_half: toast.maketext(getapplicationcontext(), "you have selected half day leave", toast.length_short).show(); break; } } }); // hide radio buttons radio_full.setvisibility(view.gone); radio_half.setvisibility(view.gone); // initiate date picker , button strdate= (textview) findviewbyid(r.id.date); // perform click event on edit text strdate.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // calender class's instance , current date , month , year calender final calendar c = calendar.getinstance(); int myear = c.get(calendar.year); // current year int mmonth = c.get(calendar.month); // current month int mday = c.get(calendar.day_of_month); // current day // date picker dialog datepickerdialog = new datepickerdialog(leave.this, new datepickerdialog.ondatesetlistener() { @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { // set day of month , month , year value in edit text // assign value date1 date1 = dayofmonth + "/" + (monthofyear + 1) + "/" + year; strdate.settext(dayofmonth + "/" + (monthofyear + 1) + "/" + year); // check if date2 has been set , compare date1 if (!textutils.isempty(date2)) { // difference getdifferencedays(date, date2 ); if(date1.equals(date2)) { // pop radio button radio_full.setvisibility(view.visible); radio_half.setvisibility(view.visible); } else { // hide radio buttons radio_full.setvisibility(view.gone); radio_half.setvisibility(view.gone); } } } }, myear, mmonth, mday); datepickerdialog.show(); } }); // initiate date picker , button strdate2 = (textview) findviewbyid(r.id.date2); // perform click event on edit text strdate2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // calender class's instance , current date , month , year calender final calendar c = calendar.getinstance(); int myear = c.get(calendar.year); // current year int mmonth = c.get(calendar.month); // current month int mday = c.get(calendar.day_of_month); // current day // date picker dialog datepickerdialog2 = new datepickerdialog(leave.this, new datepickerdialog.ondatesetlistener() { @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { // set day of month , month , year value in edit text // assign value date2 date2 = dayofmonth + "/" + (monthofyear + 1) + "/" + year; strdate2.settext(dayofmonth + "/" + (monthofyear + 1) + "/" + year); // check if date2 has been set , compare date1 if (!textutils.isempty(date1)) { // difference getdifferencedays(date, date2); if(date1.equals(date2)) { // pop radio button radio_full.setvisibility(view.visible); radio_half.setvisibility(view.visible); } else { // hide radio buttons radio_full.setvisibility(view.gone); radio_half.setvisibility(view.gone); } } } }, myear, mmonth, mday); datepickerdialog2.show(); } }); no_of_days.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { simpledateformat format = new simpledateformat("dd/mm/yyyy"); string date01 = strdate.gettext().tostring(); string date02 = strdate2.gettext().tostring(); try { date d = format.parse(date01); date d1 = format.parse(date02); getdifferencedays(d, d1); } catch (parseexception e) { e.printstacktrace(); } } }); } private void getdifferencedays(textview d, string d1) { } public void getdifferencedays(date d1, date d2) { int daysdiff = 0; long diff = d2.gettime() - d1.gettime(); long diffdays = diff / (24 * 60 * 60 * 1000) + 1; daysdiff = (int) diffdays; no_of_days.settext(integer.tostring(daysdiff)); system.out.println("day count=>" + daysdiff); }
}
pate following code in both date picker function
simpledateformat format = new simpledateformat("dd/mm/yyyy"); try { date d = format.parse(date1); date d1 = format.parse(date2); getdifferencedays(d, d1); } catch (parseexception e) { e.printstacktrace(); }
instead of calling method getdifferencedays(date, date2);
Comments
Post a Comment