kramer65
kramer65

Reputation: 53873

How to have multiple datepickers on one Android screen?

I created one screen in my Android app which contains a datepicker like so:

    TextView userDateView = (TextView) getView().findViewById(R.id.text_user_date);
    userDateView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatePickerFragment newFragment = new DatePickerFragment();
            newFragment.setListner(OverviewFragment.this);
            newFragment.show(getFragmentManager(), "datePicker");
        }
    });

and I created a callback which receives the date back and updates the userDateView, which all works fine. In another screen I now want to have two dates next to eachother which the user can both select. Unfortunately the callback doesn't know from which of the two dates the user started the datepicker fragment.

Does anybody know how I can somehow know from which button the datepickerFragment was started? All tips are welcome!

Upvotes: 0

Views: 1314

Answers (2)

Bozic Nebojsa
Bozic Nebojsa

Reputation: 3706

Try something like this:

DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListner(new DatePickerDialog.OnDateSetListener() {
    @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)    {}
    });
newFragment.show(getFragmentManager(), "datePicker");

Upvotes: 0

ivan.aguirre
ivan.aguirre

Reputation: 510

A simple approach is to supply a parameter to each DatePickerFragment and send this parameter back to your callback, like requestCode on Activity.onActivityResult.

See how to supply parameters to Fragments: http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle)

Btw, you can find a different approach here:

Multiple DatePickers in same activity

Upvotes: 1

Related Questions