My DialogFragment class is invoked when I press a button on a activity. I want the date set through this DialogFragment to be displayed on the buttons text. FindViewById re
Create Class for DatePicker Fragment:
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 Fragment When clicked On ImageButton:
/** Create DatePicker dialog on click of OrderDateEditText **/
orderDatePickerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
In Month there is plus 1 because DatePicker default takes month from 0 to 11 for Jan to Dec.
Thank you..