android - Date picker. How to set it for different State -
i have created date picker. fragment:
import java.util.calendar; import my.project.mysimplecal.mainactivity.datedialogfragmentlistener; import android.app.datepickerdialog; import android.app.dialog; import android.app.dialogfragment; import android.content.context; import android.os.bundle; import android.widget.datepicker; public class datedialogfragment extends dialogfragment { public static string tag = "datedialogfragment"; static context mcontext; static int myear; static int mmonth; static int mday; static datedialogfragmentlistener mlistener; public static datedialogfragment newinstance(context context, datedialogfragmentlistener listener, calendar now) { datedialogfragment dialog = new datedialogfragment(); mcontext = context; mlistener = listener; myear = now.get(calendar.year); mmonth = now.get(calendar.month); mday = now.get(calendar.day_of_month); bundle args = new bundle(); args.putstring("title", "set date"); dialog.setarguments(args); return dialog; } public dialog oncreatedialog(bundle savedinstancestate) { return new datepickerdialog(mcontext, mdatesetlistener, myear, mmonth, mday); } private datepickerdialog.ondatesetlistener mdatesetlistener = new datepickerdialog.ondatesetlistener() { @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { myear = year; mmonth = monthofyear; mday = dayofmonth; mlistener.updatechangeddate(year, monthofyear, dayofmonth); } }; }
and main activity
import java.util.calendar; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.compoundbutton; import android.widget.textview; import android.widget.togglebutton; import android.app.fragmenttransaction; import android.graphics.drawable.drawable; public class mainactivity extends activity { datedialogfragment frag; button btnpickdate; compoundbutton btncyclestart; compoundbutton btncyclestop; compoundbutton btnpillstart; compoundbutton btnpillstop; calendar now; textview textview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); = calendar.getinstance(); btnpickdate = (button) findviewbyid(r.id.pickdate); btncyclestart = (compoundbutton) findviewbyid(r.id.cyclestart); btncyclestop = (compoundbutton) findviewbyid(r.id.cyclestop); btnpillstart = (compoundbutton) findviewbyid(r.id.pillstart); btnpillstop = (compoundbutton) findviewbyid(r.id.pillstop); textview = (textview) findviewbyid(r.id.showdate); textview.settext(string.valueof(now.get(calendar.day_of_month)) + "-" + string.valueof(now.get(calendar.month) + 1) + "-" + string.valueof(now.get(calendar.year))); //set calendar button on listening btnpickdate.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { showdialog(); } }); //set compound button on listening , //set 1 alternative another. if start //clicked stop cannot clicked btncyclestart .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { btncyclestop.setchecked(false); } } }); btncyclestop .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { btncyclestart.setchecked(false); } } }); btnpillstart .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { btnpillstop.setchecked(false); } } }); btnpillstop .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { btnpillstart.setchecked(false); } } }); } //set text fragment class choice public void showdialog() { fragmenttransaction ft = getfragmentmanager().begintransaction(); frag = datedialogfragment.newinstance(this, new datedialogfragmentlistener() { public void updatechangeddate(int year, int month, int day) { textview.settext(string.valueof(day) + "-" + string.valueof(month + 1) + "-" + string.valueof(year)); now.set(year, month, day); } }, now); frag.show(ft, "datedialogfragment"); } // listener between date dialog fragment , // activity update buttons date public interface datedialogfragmentlistener { public void updatechangeddate(int year, int month, int day); } }
how set different format date shown, depending on state user living ? example in italy people used see day/month/year in usa used see month/day/year. hope able , willing answer because i've searched hours without success.
instead of doing own formatting calendar
fields, use dateformat.getdateinstance()
dateformat
object appropriate current locale. use format()
format date
string then. can use calendar
convert between year/month/day fields , date
.
Comments
Post a Comment