How can I create date and time settings dialog in Android? I want:
Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components.
Android Time Picker allows you to select the time of day in either 24 hour or AM/PM mode. The time consists of hours, minutes and clock format. Android provides this functionality through TimePicker class.
On your Android phone or tablet, visit the Google Calendar page on Google Play. Tap Install. Open the app and sign in with your Google Account.
Create buttons that you'll use to both display the date and time, and trigger the date and time dialogs.
Example code :
fromDatePicker = (Button) findViewById(R.id.fromDatePicker);
fromDatePicker.setText(dateNow);
fromDatePicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(FROM_DATE_DIALOG_ID);
}
});
fromTimePicker = (Button) findViewById(R.id.fromTimePicker);
fromTimePicker.setText(timeNow);
fromTimePicker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(FROM_TIME_DIALOG_ID);
}
});
Create your dialogs like this :
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case FROM_TIME_DIALOG_ID:
return new TimePickerDialog(this,fromTimeSetListener, fromHour, fromMinute, false);
case FROM_DATE_DIALOG_ID:
return new DatePickerDialog(this,fromDateSetListener,fromYear, fromMonth, fromDay);
case TO_TIME_DIALOG_ID:
return new TimePickerDialog(this,toTimeSetListener, toHour, toMinute, false);
case TO_DATE_DIALOG_ID:
return new DatePickerDialog(this,toDateSetListener,toYear, toMonth, toDay);
}
return null;
}
Your screen will look like this :
When clicking the date button, a datepicker will popup looking like this :
When clicking the time button, a timepicker will popup looking like this :
Android doesn't offer a combined DateTimePicker dialog out of the box... If you want to combine both the date and timepicker on 1 screen or dialog you have 3 options :
Work with the UI widgets in a layout/view :
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Find a third party component offering this functionality
There's a custom datetimepicker at http://code.google.com/p/datetimepicker
Write one yourself
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With