Implementing DatePicker in Fragment

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.

  • How to make an Android launcher icon
  • separation between rows in table layout
  • Disable Word Wrap in an Android Multi-Line TextView
  • How to fire onListItemClick in Listactivity with buttons in list?
  • Multiple dex files define Lcom/google/firebase/FirebaseException
  • uSTL or STLPort for Android?
  • 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);
                   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(month + 1)
            buttonDate = (Button)rootView.findViewById(;

    How to implement DatePicker in Fragment?

    Related posts:

    When should I use ACCESS_COARSE_LOCATION permission?
    Does Android KitKat allows devices that support Bluetooth LE to act as a peripheral device?
    Javascript to detect mobile browser screen width?
    Build Unsigned APK with Gradle
    Rate Limit Exceeded error when using Google Cloud Messaging API
    Sending SMS to real test device (not emulator) via DDMS or emulator instance
  • Cannot resolve symbol “maps”
  • Run multiple android app instances like parallel space
  • Google Play Services example: “connectionResult cannot be resolved”
  • How to change the status bar notification icons' color/tint in android (marshmallow and above 23+)?
  • Determining if an Android device is rooted programmatically?
  • Android Studio Gradle project “Unable to start the daemon process /initialization of VM”
  • 3 Solutions collect form web for “Implementing DatePicker in Fragment”

    Use a DialogFragment

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

    On Button click

    DialogFragment picker = new DatePickerFragment();, "datePicker");

    public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {
    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);
    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());    

    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.

    . . . 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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.