Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);     } 
like image 496
Gonzalo Cao Avatar asked Jul 17 '12 16:07

Gonzalo Cao


People also ask

Where do you create a DialogFragment for a date picker?

To define a DialogFragment for a DatePickerDialog , you must: Define the onCreateDialog() method to return an instance of DatePickerDialog. Implement the DatePickerDialog. OnDateSetListener interface to receive a callback when the user sets the date.

How to set selected date in DatePicker in Android?

Implement OnDateSetListener of DatePickerDialog class and override onDateSet() method. The onDateSet() method will set the date after selection in the tvDate TextView. In the onClick() method implement setOnClickListener of btPickDate. Create an instance of DatePicker(our class).

How to select date in Android?

In Android, DatePicker is a widget used to select a date. It allows to select date by day, month and year in your custom UI (user interface). If we need to show this view as a dialog then we have to use a DatePickerDialog class. For selecting time Android also provides timepicker to select time.

Which one is used for Date Picker Dialog?

We will use DatePickerDialog and TimePickerDialog classes with Calendar class in our android application code to achieve this.


1 Answers

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     }     ... } 
like image 134
Leszek Avatar answered Oct 14 '22 13:10

Leszek