2
\$\begingroup\$

I'm developing an Android activity which asks user to enter start and end dates and times in order to know a certain period of time. After picking start date, user picks start time, and then he/she picks end date and time. I have TimePickers and DatePickers showing up one after another by setting flags, and I feel like my approach is a bit clumsy. I'd like to know if there's better one.

MainActivity.java

public class MainActivity extends AppCompatActivity implements
        DeviceDialogFragment.DeviceDialogListener, TimePickerDialog.OnTimeSetListener,
        DatePickerDialog.OnDateSetListener {
    private static final int START_DATE = 1;
    private static final int START_TIME = 2;
    private static final int FINAL_DATE = 3;
    private static final int FINAL_TIME = 4;

    DeviceDialogFragment deviceDialogFragment;
    int dateElementWritten;
    DatePickerFragment datePickerFragment;
    TimePickerFragment timePickerFragment;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case (R.id.action_date):
                datePickerFragment = new DatePickerFragment();
                dateElementWritten = START_DATE;
                datePickerFragment.show(getSupportFragmentManager(), "datePicker");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    int startYear, startMonth, startDay;
    int finalYear, finalMonth, finalDay;
    @Override
    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
        if(dateElementWritten == START_DATE){
            startYear = i;
            startMonth = i1;
            startDay = i2;
            dateElementWritten = START_TIME;
            timePickerFragment = new TimePickerFragment();
            timePickerFragment.show(getSupportFragmentManager(), "timePicker");
        } else if(dateElementWritten == FINAL_DATE){
            finalYear = i;
            finalMonth = i1;
            finalDay = i2;
            dateElementWritten = FINAL_TIME;
            timePickerFragment = new TimePickerFragment();
            timePickerFragment.show(getSupportFragmentManager(), "timePicker");
        }
    }

    int startHour, startMinute;
    int finalHour, finalMinute;
    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        if(dateElementWritten == START_TIME){
            startHour = i;
            startMinute = i1;
            dateElementWritten = FINAL_DATE;
            datePickerFragment = new DatePickerFragment();
            datePickerFragment.show(getSupportFragmentManager(), "datePicker");
        } else if(dateElementWritten == FINAL_TIME){
            finalHour = i;
            finalMinute = i1;
        }
    }
}

TimePickerFragment.java

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

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new android.app.TimePickerDialog(getActivity(),(MainActivity) getActivity(), 0,0, true);
    }
}

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment {
    OnDateSetListener dateListener;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            dateListener = (OnDateSetListener) activity;
        }catch (ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implement OnDateSetListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        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);

        return new android.app.DatePickerDialog(getActivity(),(MainActivity) getActivity(), year,month,day);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.