6

I have an activity where I need to accept two dates, a start date and an end date. I have two buttons, which when clicked would display the date pickers. After entering the date, I need to store this date using SharedPreferences. I am using this tutorial:

http://developer.android.com/guide/topics/ui/controls/pickers.html

I have seen questions on two date pickers but none of them are using DialogFragment. They are using deprecated functions, and I don't really want to use those, since I hear it's better to use DialogFragment.

Now how do I go about TWO date pickers? How do I know which button was clicked (startButton or endButton) in the onDateSet function? Is there some way to store the view id in the DialogFragment I create on button click, so that I can access it in onDateSet?

Any help would be great, thanks.

Vedavyas Bhat
  • 2,068
  • 1
  • 21
  • 31

3 Answers3

11

It can be done like that:

View.OnClickListener showDatePicker = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final View vv = v;

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                if (vv.getId() == R.id.StartDate //id of your StartDate button) {
                        //do the stuff
                } else //EndDate button was clicked {
                        //do the stuff
                }
            }
        }, year, month, day);
        datePickerDialog.show();
    }
};
startDate.setOnClickListener(showDatePicker);
endDate.setOnClickListener(showDatePicker);

The main idea here is to store the View fired OnClickEvent (in your case buttons) and compare ID of that view with IDs of your buttons

nikis
  • 11,166
  • 2
  • 35
  • 45
  • The comments on the if else have the a close round brack missing for the if ) open curly { missing. Else has missing open curly brace { – user1821961 Jun 01 '18 at 03:38
1

To avoid this kind of problem you can create inner classes that implement the interface, think good OOP, so you can do everything you want without checking if it was the first or second for example:

class innerFirstDate implements DatePickerDialog.OnDateSetListener{
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            //Doing thing with first Date pick Dialog
        }

 }

class innerSecondDate implements DatePickerDialog.OnDateSetListener{
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            //Doing thing with second Date Picker Dialog
        }

 }

Later you can do for first one:

DatePickerDialog firstD = new DatePickerDialog(getActivity(), new innerFirstDate(), year, month, day).show();

And for second one:

DatePickerDialog secondD = new DatePickerDialog(getActivity(), new innerSecondDate(), year, month, day).show();

Everything become much clear

digiwizkid
  • 791
  • 2
  • 14
  • 28
Luis Pena
  • 4,132
  • 2
  • 15
  • 23
  • I thought of this, yes, but I thought the more efficient way to do it would be using one inner class only for both pickers, right? – Vedavyas Bhat Feb 18 '14 at 17:07
  • You can too, but doing it with only 1 inner class can be confusing after you have alot of code and checking if was first or second and using flag make it even more. Remember keep it easy but efficient lol – Luis Pena Feb 18 '14 at 17:10
  • Also creating 2 different inner classes for this isn't a huge hit for perfomance, so there's no problem doing it – Luis Pena Feb 18 '14 at 17:11
0
public void onClick(View v) {
    d1= 1;
    d2=2;
    DatePickerDialog dialog = new DatePickerDialog(this, this, 2013, 2, 18);
    dialog.show();
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    if(d1==1) {
        e2.setText(Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year));
    }
    if(d2==1) {
        e3.setText(Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year));
    }
}

public void onClick1(View v) {
    d2=1;
    d1=2;
    DatePickerDialog dialog = new DatePickerDialog(this, this,2013,2, 18);
    dialog.show();
}

its working perfectly