Get date from datepicker using dialogfragment
I'm using the google example to insert a datepicker inside my app using a dialogfragment
http://developer.android.com/guide/topics/ui/controls/pickers.html
But I'm not sure how to get date after set it (not java expert). Dialog and datepicker runs ok and I can log that date is correctely set but, how can i do to get a callback executed on parent activity?
This is my Dialog fragment
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
**Log.w("DatePicker","Date = " + year);**
}
}
...and I call dialog from my activity with...
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
What is the correct way to call a method in my parent activity instead Log.w? I suppose that is something related with passing a callback as parameter or something but most of references I found on internet are about previous versions without dialogfragments
EDIT: not sure if it's important but parent activity is declared as:
public class EditSessionActivity extends FragmentActivity {
SOLUTION: thanks to Lecho user this is the way to do it
DatePickerFragmennt.class
public class DatePickerFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
}
}
...and parent activity EditSessionActivity.class...
public class EditSessionActivity extends FragmentActivity implements OnDateSetListener {
...
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((EditText) findViewById(R.id.tf_date)).setText("Date = " + year);
}
Constructor fo DatePickerDialog
takes DatePickerDialog.OnDateSetListener
as second parameter, so maybe you should implement that interface in your parent activity EditSessionActivity
(not in DatePickerFragment
) and change this line:
return new DatePickerDialog(getActivity(), this, year, month, day);
into this:
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
And then your activity should looks like this:
public class EditSessionActivity extends FragmentActivity implements
DatePickerDialog.OnDateSetListener{
public void onDateSet(DatePicker view, int year, int month, int day) {
//use date in your activity
}
...
}
I simple override onDateSet in date picker creation in my class that shows a date picker. See code below:
private OnClickListener onDateClicked() {
return new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
salePaymentCustomView.setBtDateText("" + day + "/" + month+1 + "/" + year);
}
};
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
};
}
The problem can also be solved without moving the onDateSet
method to the parent activity. You just have to tell the DatePickerFragment
where to search for the view:
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((TextView) getActivity().findViewById(R.id.tf_date)).setText("Date = " + year);
}
}
I also changed the target of the cast from EditText
to TextView
. This way, you can reuse your implementation for different kinds of views, not only for an EditText
.
i have one correction to the above answer. instead of return the DatePickerDialog like this
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
you should return it in a more generic way
return new DatePickerDialog(getActivity(), (OnDateSetListener)getActivity(), year, month, day);
just make sure your activity implements OnDateSetListener interface and override the function onDateSet
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)