Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two date pickers in same activity android

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.

like image 590
Vedavyas Bhat Avatar asked Feb 18 '14 16:02

Vedavyas Bhat


2 Answers

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

like image 110
nikis Avatar answered Oct 31 '22 16:10

nikis


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

like image 30
Luis Pena Avatar answered Oct 31 '22 18:10

Luis Pena