Get date with datepicker using dialogfragment

I am using a google example to insert a datepicker inside my application using dialogfragment
http://developer.android.com/guide/topics/ui/controls/pickers.html

But I'm not sure how to get the date after setting it (not a java expert). Dialog and datepicker are working fine and I can register this date, but can I do to make a callback in parent activity?

This is my piece of dialogue.

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 invoke a dialogue 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 of Log.w? I believe this is due to passing the callback as a parameter or something else, but most of the links that I found on the Internet relate to previous versions without dialog fragments

EDIT: not sure if important but parent activity is declared as:

 public class EditSessionActivity extends FragmentActivity { 

SOLUTION: thanks to user Lecho 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); } 
+48
android datepicker
Jul 17 2018-12-17T00:
source share
7 answers

The fo DatePickerDialog takes DatePickerDialog.OnDateSetListener as the second parameter, so maybe you should implement this interface in your parent EditSessionActivity (not in DatePickerFragment ) and change this line:

 return new DatePickerDialog(getActivity(), this, year, month, day); 

in it:

 return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day); 

And then your activity should look 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 } ... } 
+59
Jul 17 2018-12-17T00:
source share

I just override onDateSet in creating a date in my class that shows the date picker. See the following code:

  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"); } }; } 
+25
Feb 07 '13 at
source share

The problem can also be solved without moving the onDateSet method to parent activity. Just tell DatePickerFragment where to look for the search:

 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 cast target from EditText to TextView . Thus, you can reuse your implementation for different types of views, and not just for EditText .

+13
May 11 '13 at 23:14
source share

I have one amendment to the above answer. instead of returning a DatePickerDialog like this

returns a new DatePickerDialog (getActivity (), (EditSessionActivity) getActivity (), year, month, day);

you should return it in a more general way

returns a new DatePickerDialog (getActivity (), (OnDateSetListener) getActivity (), year, month, day);

just make sure your activity implements the OnDateSetListener interface and overrides the onDateSet function

 @Override public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) 
+9
Jan 25 '14 at 6:50
source share
 public void onDate(View view) { DialogFragment fragment = new SelectDateFragment(); fragment.show(getFragmentManager(), "Date Picker"); } // @SuppressLint("NewApi") class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public Dialog onCreateDialog(Bundle savedInstanceState) { System.out.println("entrering on create dialog");; return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay);//it will return dialog setting date with mYera,MMonth and MDay } @Override public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { System.out.println("year=" + year + "day=" + dayOfMonth + "month=" + monthOfYear); mYear=year; mMonth=monthOfYear; mDay=dayOfMonth; onPopulateSet(year, monthOfYear + 1, dayOfMonth); } // set the selected date in the edit text private void onPopulateSet(int year, int i, int dayOfMonth) { EditText et_setDate; et_setDate = (EditText) findViewById(R.id.register_et_dob);//register_et_dob:-id name of the edit text et_setDate.setText(dayOfMonth + "/" + i + "/" + year); System.out.println("enetring on populate Set"); } 
+3
Aug 26 '13 at 11:13
source share

Leszek's answer works, but not if the Dialog is launched from another fragment. In this case, you need to use setTargetFragment() in the code that creates the dialog and getTargetFragment() in the code of the dialog.

In the code that creates the dialog:

 DialogFragment dialogFragment = new YourDialogFragment(); dialogFragment.setTargetFragment(YourParentFragment.this, 0); dialogFragment.show(getFragmentManager(), "mydialog"); 

In the DialogFragment code:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { YourParentFragment parentFragment = (YourParentFragment) getTargetFragment(); // Manipulate the parent fragment // ... } }; // Get the initial date // ... return new DatePickerDialog(getActivity(), listener, year, month, day); } 
+2
Feb 22 '17 at 11:44
source share

I had a similar problem, but my date picker was launched from a dialog fragment inside another fragment. This made it difficult to work with callbacks because they want to return to the main operation, and I wanted the data to return to the previous fragment of the dialog.

First, I passed the new date values ​​to the main activity (using OnDateSetListener) and was going to get them using the dialog box that launched the date picker, but there are no lifecycle events in this dialog box when the datepicker closes.

As a result, I came to onDismiss in the date picker, create a new dialog fragment, call the result set method on it, and then run it. Of course, for this you need to make sure that the previous snippet is rejected when it starts the date picker.

This is the dialog that caused the date picker.

 public class NewTrialDialogFragment extends DialogFragment { public final String LOG_TAG = "NewTrialDialog Fragment"; Button startDateButton; Integer newYear, newMonth, newDay; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.dialog_new_trial, container, false); getDialog().setTitle("Dialog New Trial"); startDateButton = (Button) rootView.findViewById(R.id.button_start_date); startDateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDatePickerDialog(v); } }); //If you exit the datepicker without choosing anything, it returns zeros if (newYear != null && newMonth != null && newDay != null && newYear != 0) { startDateButton.setText(newYear + " " + newMonth + " " + newDay); }else{ startDateButton.setText("Select Start Date"); } return rootView; } public void showDatePickerDialog(View v) { TrialDatePickerDialog newDialog = new TrialDatePickerDialog(); newDialog.show(getActivity().getSupportFragmentManager(), "datePicker"); this.dismiss(); } public void setDates(int year, int month, int day){ newYear = year; newMonth = month; newDay = day; }} 

And date picker

 public class TrialDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener { private final String LOG_TAG = "TrialDatePickerDialog"; int newYear, newMonth, newDay; @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 TrialDatePickerDialog and return it return new DatePickerDialog(getActivity(), this , year, month, day); } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); FragmentManager fm = getActivity().getSupportFragmentManager(); NewTrialDialogFragment newTrialDialogFragment = new NewTrialDialogFragment(); newTrialDialogFragment.setDates(newYear, newMonth, newDay); newTrialDialogFragment.show(fm, "new_trial_dialog"); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { newYear = year; newMonth = monthOfYear; newDay = dayOfMonth; } } 
0
Apr 17 '16 at 8:19
source share



All Articles