11

I'am using Fragment which is an instance of Fragment and not of DialogFragment.

I did google most of the search result shows how to use DialogFragment to have DatePicker.

which isn't working in my case because of type mismatch of Fragment and DialogFragment

Any example or idea would be helpful

Here is the Fragment Java Code

        public class CreateReportFragment extends Fragment {

        public CreateReportFragment(){}

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {

               rootView = inflater.inflate(R.layout.activity_create_report, container, false);
               initViews();
               return rootView;
        }

        private void initViews() 
       {

         final Calendar c = Calendar.getInstance();
             year  = c.get(Calendar.YEAR);
             month = c.get(Calendar.MONTH);
             day   = c.get(Calendar.DAY_OF_MONTH);

             editTextDate.setText(new StringBuilder()
             // Month is 0 based, just add 1
             .append(year)
             .append("-")
                    .append(month + 1)
                     .append("-").append(day));

        buttonDate = (Button)rootView.findViewById(R.id.buttonDate);
        }

How to implement DatePicker in Fragment?

AndroidNewbie
  • 669
  • 3
  • 16
  • 23

3 Answers3

33

Use a DialogFragment

If i am guessing right you want to show a DatePicker on click of buttonDate

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

On Button click

DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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); 

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());    
}
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 2
    where did **listener = (TheListener) getActivity();** come from? – AndroidNewbie Jan 04 '14 at 17:14
  • @AndroidNewbie remove it it was a copy paste from one of my codes and i forgot to remove it – Raghunandan Jan 04 '14 at 17:17
  • @AndroidNewbie you could use a interface as a callback to the activity and then communicate date to fragment. http://developer.android.com/training/basics/fragments/communicating.html – Raghunandan Jan 04 '14 at 17:24
  • @AndroidNewbie first look at the docs understand and give it a try. then if you have problems come back and post the updated code – Raghunandan Jan 04 '14 at 17:39
  • 1
    Sir. Please? Im stuck here again. – AndroidNewbie Jan 04 '14 at 18:02
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44508/discussion-between-raghunandan-and-androidnewbie) – Raghunandan Jan 04 '14 at 18:03
  • @Raghunandan I tried your code its working fine, except for when i click button twice (in quick succession) i get `java.lang.IllegalStateException: Fragment already added` exception. How to stop it from showing twice? – Kaustuv Jul 23 '14 at 15:37
  • @Kaustuv you can disable the button after first click and enable it later. Its upto you how you design – Raghunandan Jul 23 '14 at 15:40
  • @Raghunandan yes i can disable the button on first click and enable it when i get a callback in onDateSet method. The problem is, in my device the date picker has a 'cancel' button. so when user clicks on cancel onDateSet method is not called. what to do? – Kaustuv Jul 23 '14 at 15:44
  • @Kaustuv you can use interface as a callback to the activity and do what is required. Ask a new question instead of commenting here – Raghunandan Jul 23 '14 at 15:49
  • @Raghunandan doing that only, onDateSet method calls a interface method to my fragment. but since onDateSet is not called (when cancel button of date picker is used), my fragment doesn't know when to enable back the button. if user click on 'set' button of date picker then all is fine. Is there any way to find if date picker fragment is already on screen or not? Something similar to like we can do on a listView.getAdapter ? – Kaustuv Jul 23 '14 at 15:59
  • @Kaustuv ask a new question. i am not going to comment here anymore. good luck. Understand fragment and activity and do your design. Its a design issue. Do look at the fragment api's once. Have you done that?? – Raghunandan Jul 23 '14 at 16:00
  • please help me here http://stackoverflow.com/questions/42779110/android-spinner-doesnt-showing-last-selected-item/ – Pranav MS Mar 15 '17 at 06:03
  • how can use the new value in the main fragment ? can i return it or something ? – mrid Aug 08 '17 at 10:50
5

How to implement DatePicker in Fragment?

The same way you would "implement" a TextView in a Fragment: have onCreateView() return a View that is or contains a DatePicker. For example, you might have a layout file that contains a DatePicker widget, and have onCreateView() inflate that layout.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

. . . which isn't working in my case because of type mismatch of Fragment and DialogFragment

A DialogFragment IS-A Fragment because it extends Fragment.Therefore, you can use a DialogFragment anywhere you would a Fragment.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132