Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date AND time settings dialog in Android

Tags:

android

How can I create date and time settings dialog in Android? I want:

  • 3 fields - day, hours and minutes
  • it should look native to the system - that makes writing my own widget quite hard - how to make the plus/minus buttons above/below each field look like the standard buttons that are in TimePickerDialog? I mean even on HTC Sense.
like image 908
fhucho Avatar asked Nov 23 '10 20:11

fhucho


People also ask

What is date and time picker in Android?

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.

What is the use of TimePicker dialog?

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.

How do I open an Android calendar?

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.


1 Answers

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 :

alt text

When clicking the date button, a datepicker will popup looking like this :

alt text

When clicking the time button, a timepicker will popup looking like this :

alt text

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

like image 200
ddewaele Avatar answered Oct 19 '22 21:10

ddewaele