android - Validate date picker dialog for birthday -
public void onclick(view view) { calendar c = calendar.getinstance(); int cyear = c.get(calendar.year); int cmonth = c.get(calendar.month); int cday = c.get(calendar.day_of_month); datepickerdialog dialog = new datepickerdialog(students.this,new ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { string date_selected = string.valueof(dayofmonth)+" /"+string.valueof(monthofyear+1)+" /"+string.valueof(year); datefieldtextbox.settext(date_selected); // campusnewsisactivity.showlog("datefieldtextbox selected"); } },cyear,cmonth,cday); // dialog.setcancelable(false); dialog.show(); } want validate datepickerdialof bday ie. should not accept birthdate before 1st jan 1985
package com.example.pickerdate; import java.util.calendar; import android.app.activity; import android.app.datepickerdialog; import android.app.dialog; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.datepicker; import android.widget.datepicker.ondatechangedlistener; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private textview mdatedisplay; private int myear; private int year1; private int mmonth; private int mday; private int month; private int day; static final int date_dialog_id = 1; button pickdate; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mdatedisplay = (textview) findviewbyid(r.id.datedisplay); pickdate = (button) findviewbyid(r.id.pickdate); pickdate.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { system.out.println("hello3"); showdialog(date_dialog_id); } }); system.out.println("hello1"); final calendar c = calendar.getinstance(); myear = c.get(calendar.year); mmonth = c.get(calendar.month); mday = c.get(calendar.day_of_month); updatedisplay(); year1 = myear; month = mmonth; day = mday; } @override protected dialog oncreatedialog(int id) { datepickerdialog _date = null; switch (id) { case date_dialog_id: _date = new datepickerdialog(this, mdatesetlistener, myear, mmonth, mday) { @override public void ondatechanged(datepicker view, int year, int monthofyear, int dayofmonth) { system.out.println("----------ondatechanged()-----------" + myear + "--" + year); system.out.println("----------ondatechanged()-----------" + mmonth + "--" + monthofyear); system.out.println("----------ondatechanged()-----------" + mday + "--" + dayofmonth); /* * these lines of commented code used setting * maximum date on date picker.. * * if (year > myear && year) view.updatedate(myear, mmonth, * mday); * * if (monthofyear > mmonth && year == myear ) * view.updatedate(myear, mmonth, mday); * * if (dayofmonth > mday && year == myear && monthofyear == * mmonth) view.updatedate(myear, mmonth, mday); */ // these below lines of code used setting maximum // minimum dates on date picker dialog.. if ((myear > year) || ((mmonth > monthofyear) && (myear == year)) || ((mday > dayofmonth) && (myear == year) && (mmonth == monthofyear))) { view.updatedate(year1, month, day); } } }; } system.out.println("hello5"); return _date; } protected void onpreparedialog(int id, dialog dialog) { switch (id) { case date_dialog_id: system.out.println("hello6"); ((datepickerdialog) dialog).updatedate(myear, mmonth, mday); break; } } private void updatedisplay() { system.out.println("hello2"); mdatedisplay.settext(new stringbuilder() // month 0 based add 1 .append(mmonth + 1).append("-").append(mday).append("-") .append(myear).append(" ")); } private datepickerdialog.ondatesetlistener mdatesetlistener = new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { system.out.println("hello7"); myear = year; mmonth = monthofyear; mday = dayofmonth; system.out.`enter code here`println("year=" + year); system.out.println("month=" + monthofyear); system.out.println("day=" + dayofmonth); updatedisplay(); } }; }
Comments
Post a Comment