I have an activity where I need to accept two dates, a start date and an end date. I have two buttons, which when clicked would display the date pickers. After entering the date, I need to store this date using SharedPreferences. I am using this tutorial:
http://developer.android.com/guide/topics/ui/controls/pickers.html
I have seen questions on two date pickers but none of them are using DialogFragment. They are using deprecated functions, and I don't really want to use those, since I hear it's better to use DialogFragment.
Now how do I go about TWO date pickers? How do I know which button was clicked (startButton or endButton) in the onDateSet function? Is there some way to store the view id in the DialogFragment I create on button click, so that I can access it in onDateSet?
Any help would be great, thanks.
It can be done like that:
View.OnClickListener showDatePicker = new View.OnClickListener() {
@Override
public void onClick(View v) {
final View vv = v;
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (vv.getId() == R.id.StartDate //id of your StartDate button) {
//do the stuff
} else //EndDate button was clicked {
//do the stuff
}
}
}, year, month, day);
datePickerDialog.show();
}
};
startDate.setOnClickListener(showDatePicker);
endDate.setOnClickListener(showDatePicker);
The main idea here is to store the View
fired OnClickEvent
(in your case buttons) and compare ID of that view with IDs of your buttons