Search code examples
javaandroidandroid-fragmentsdialogtimepicker

How to get the time of the TimePickerDialog in the EditText in another fragment?


I am trying to get the time of my TimePickerDialog to an EditText in the FinishPostFragment.

I am showing the selected time right now in a Toast but how can I show the data in the EditText in the FinishPostFragment?

FinishPostFragment in my onCreateView:

timeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // view TimePickerDialog
            showTimeDialog(v);
        }
    });

FinishPostFragment outside my onCreateView:

public void showTimeDialog(View v){
    TimeDialogFragment tdf = new TimeDialogFragment();
    tdf.show(getFragmentManager().beginTransaction(), "TimePickerFragment");
}

TimePickerDialog.OnTimeSetListener test = new TimePickerDialog.OnTimeSetListener(){
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        time_finish.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
    }
};

TimeSettings.java:

public class TimeSettings implements TimePickerDialog.OnTimeSetListener {
    Context context;

    public TimeSettings(Context context){
        this.context = context;
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(context, "Selected time is hour: " +hourOfDay+ " minute: " + minute, Toast.LENGTH_LONG).show();
    }
}

TimeDialogFragment:

public class TimeDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        Calendar calendar = Calendar.getInstance();

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        TimePickerDialog dialog;

        TimeSettings timeSettings = new TimeSettings(getActivity());
        dialog = new TimePickerDialog(getActivity(), timeSettings, hour, minute,
                android.text.format.DateFormat.is24HourFormat(getActivity()));

        return dialog;
    }
}

Solution

  • Something close to what you have that might work is the following. Have your FinishPostFragment implement TimePickerDialog.OnTimeSetListener itself instead.

    Show the TimeDialaogFragment using getChildFragmentManager() instead.

    tdf.show(getChildFragmentManager().beginTransaction(), "TimePickerFragment");
    

    That will make it so you can then do the following in your TimeDialogFragment.onCreateView to send the selected time directly to the FinishPostFragment.

    TimePickerDialog.OnTimeSetListener timeSetListener = 
            (TimePickerDialog.OnTimeSetListener) getParentFragment();
    
    dialog = new TimePickerDialog(getActivity(), timeSetListener, hour, minute,
            android.text.format.DateFormat.is24HourFormat(getActivity()));
    

    Note in order to use getParentFragment() you must be using the support library or targeting API 17+.