Create a class for the DatePicker snippet:
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Calendar now = Calendar.getInstance(); year = now.get(Calendar.YEAR); month = now.get(Calendar.MONTH); day = now.get(Calendar.DATE); return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { orderDateEditText.setText(year + "-" + (month + 1) + "-" + day); } }
Call this snippet When you click the ImageButton button:
orderDatePickerButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {
There is plus 1 in a Month , because by default DatePicker accepts a month from 0 to 11 for the period from January to December.
Thanks..
source share