Search code examples
javaandroidandroid-dialogfragmentandroid-timepicker

Retrieving times from two TimePickers in DialogFragments


I have an input form in an activity where I'd like to retrieve the start time and end time of an event. I am using a timepicker displayed in a dialog fragment to get the start time and return it to the main activity, but how do re use the same logic to retrieve the end time of the event? My code:

Main Activity:

public class AddEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
private int pickerHour = 0;
private int pickerMin = 0;

//code ommitted for length

public void showTimePickerDialog(View v) {
    TimePickerFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    String amOrPm = "am";
    pickerHour = hourOfDay;
    pickerMin = minute;
    String minutesString = "";
    if (hourOfDay > 12){ //translate to 12 hr clock. Little workaround to get both timepicker and am/pm label to show up correctly.
                amOrPm = "pm";
                pickerHour-=12;
            }
            if (pickerMin < 10) {
                minutesString = "0"; //fix weird bug where only one zero is shown on times ending in :00
            }
    startTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
}

TimePickerFragment:

public class TimePickerFragment extends DialogFragment {
int hour,minute;
private Activity mActivity;
private TimePickerDialog.OnTimeSetListener mListener;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = activity;
    try {
        mListener = (TimePickerDialog.OnTimeSetListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    return new TimePickerDialog(mActivity, mListener, hour, minute,
            DateFormat.is24HourFormat(mActivity));
}

}

I need to be able to differentiate between the start time and the end time, but I am not sure how to differentiate them without repeating myself. I am pretty new to Java and Android. Thank you so much for any help.


Solution

  • Possibly something as simple as a boolean?

    public class AddEventActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{
        ...
        boolean isSettingStartTime = true;
    
    
        //code ommitted for length
    
        public void showTimePickerDialog(View v) {
            TimePickerFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "timePicker");
        }
    
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    
            ...
    
            if (isSettingStartTime) {
                startTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
                isSettingStartTime = false;
            } else {
                endTimeView.setText(pickerHour + " : " + minutesString + pickerMin + " " +amOrPm );
            }
        }
    }