Posts

Showing posts from March, 2015

Set current date to Button and Datepicker dialog

This tutorial is going to explain how to set current date on Button and when you click on that Button, DatePicker Dialog open to ask your new Date. When you set Date from datepicker dialog, selected Date will be set to that Button. A simple Layout with just only Button.   <Button         android:id="@+id/date"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_marginTop="10dp"         android:onClick="setDate" /> For Date related task, you have to use Calendar class. And to get current Date, use method getInstance() of Calendar class. Calendar cal = Calendar.getInstance(); mYear = cal.get(Calendar.YEAR); mMonth = cal.get(Calendar.MONTH); mDay = cal.get(Calendar.DAY_OF_MONTH); mYear, mMonth, mDay is of int type. Now set date on Button: Button date = (Button) findViewById(R.id.date); date.setText( new StringBuilder().append( mDay ).append("/"