How to create a date picker in an android app -
i want show date picker on button click. there control used or need create own in onclick() method of button. if suggestion there?
thanks in advance
you try following code.
calenderactivity.java
public class calenderactivity extends activity implements ondatechangedlistener, onmonthchangedlistener { private static final dateformat formatter = simpledateformat.getdateinstance(); simpledateformat fmt = new simpledateformat("dd/mm/yyyy"); @bind(r.id.calendarview) materialcalendarview widget; @bind(r.id.textview) textview textview; button ok; int new_date,new_month; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_calender); getwindow().setlayout(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content); butterknife.bind(this); widget.setondatechangedlistener(this); widget.setonmonthchangedlistener(this); widget.setselecteddate(calendar.getinstance()); calendar c = calendar.getinstance(); system.out.println("current time => " + c.gettime()); simpledateformat df = new simpledateformat("dd/mm/yyyy"); string formatteddate = df.format(c.gettime()); textview.settext(formatteddate); // toast.maketext(getapplicationcontext(),"date :"+new_date+"/"+new_month+"/"+c.get(calendar.year),toast.length_long).show(); ok = (button) findviewbyid(r.id.ok_button); ok.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent returnintent = new intent(); returnintent.putextra("result", textview.gettext().tostring()); setresult(result_ok, returnintent); calenderactivity.this.finish(); } }); } @override public void ondatechanged(@nonnull materialcalendarview widget, @nullable calendarday date) { if(date == null) { textview.settext(null); } else { // toast.maketext(getapplicationcontext(), "selected date : " + date.getdate(), toast.length_short).show(); textview.settext(fmt.format(date.getdate())); } } @override public void onmonthchanged(materialcalendarview widget, calendarday date) { // toast.maketext(this, formatter.format(date.getdate()), toast.length_short).show(); } }
activity_calender.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".basicactivity" > <com.prolificinteractive.materialcalendarview.materialcalendarview android:id="@+id/calendarview" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/textview" android:layout_margin="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="" android:textcolor="#000000" android:textappearance="?android:attr/textappearancelarge" /> <button android:id="@+id/ok_button" android:layout_width="150dp" android:layout_height="50dp" android:text="ok" android:textcolor="@color/white" android:padding="5dp" android:background="@drawable/btn_calender_bg" android:positivebuttontext="ok" android:layout_gravity="center_horizontal"/>
@ last pass intent in "on click" of button. hope helps!!
Comments
Post a Comment