Time and Date piker both are in single dialog.
Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs.
Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date
(month, day, year). Using these pickers helps ensure that your users can pick a time or date that
is valid, formatted correctly, and adjusted to the user's locale.
We recommend that you use Dialog Fragment to host each time or date picker. The Dialog Fragment manages the dialog life-cycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large screens.
Note:
The code samples below show how to create dialogs for a time
picker and date picker using the support Library APIs for Dialog Fragment. If your app's minsdkVersion is 11 or
higher, you can instead use the platform version of Dialog Fragment.
Time Piker
- Define the
onCreateDialog()
method to return an instance ofTimePikerDialog
- Implement the
TimePikerDialog.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 chosen by the user } }
=========================================================================
Date Piker
- Define the
onCreateDialog()
method to return an instance ofDatePikerDialog
- Implement the
DatePikerDialog.
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(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen by the user } }
=============================================================================
For Source Code Click on Download.
Unable to find attachments?
ReplyDelete