To display a TimePickerDialog using DialogFragment, define a fragment class that extends DialogFragment and return a TimePickerDialog from the fragment's onCreateDialog() method.
To define a DialogFragment for a TimePickerDialog, do the following:
Define the onCreateDialog() method to return an instance of TimePickerDialog.
Implement the TimePickerDialog.OnTimeSetListener interface to receive a callback when the user sets the time.
Here's an example:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker.
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it.
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time the user picks.
}
}
After you define a DialogFragment like the one in the preceding example, you can display the time picker by creating an instance of the DialogFragment and calling the show() method.
For example, here's a button that, when tapped, calls a method to show the dialog:
<Button
android:id="@+id/pickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick time" />
When the user taps this button, the system calls the following method:
findViewById<Button>(R.id.pickTime).setOnClickListener {
TimePickerFragment().show(supportFragmentManager, "timePicker");
}
This method calls show() on a new instance of the DialogFragment defined in the preceding example. The show() method requires an instance of FragmentManager and a unique tag name for the fragment.
Creating a DatePickerDialog is like creating a TimePickerDialog. The difference is the dialog you create for the fragment.
To display a DatePickerDialog using DialogFragment, define a fragment class that extends DialogFragment and return a DatePickerDialog from the fragment's onCreateDialog() method.
To define a DialogFragment for a DatePickerDialog, do the following:
Define the onCreateDialog() method to return an instance of DatePickerDialog.
Implement the DatePickerDialog.OnDateSetListener interface to receive a callback when the user sets the date.
Here's an example:
public static 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(requireContext(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date the user picks.
}
}
See the DatePickerDialog class for information about the constructor arguments.
You just need an event that adds an instance of this fragment to your activity.
After you define a DialogFragment like the preceding example, you can display the date picker by creating an instance of the DialogFragment and calling show().
For example, here's a button that, when tapped, calls a method to show the dialog:
<Button
android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick date"/>
When the user taps this button, the system calls the following method:
findViewById<Button>(R.id.pickDate).setOnClickListener {
val newFragment = DatePickerFragment();
newFragment.show(supportFragmentManager, "datePicker");
}
This method calls show() on a new instance of the DialogFragment defined in the preceding example. The show() method requires an instance of FragmentManager and a unique tag name for the fragment.