Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a date and time picker in Android? [closed]

Is there any android widget that enable to pick the date and the time at the same time ? I already use the basic time picker and date picker.

But they are not that sexy and user friendly (I found). Do you know if a widget including both date and time exists?

Thanks a lot, Luc

like image 253
Luc Avatar asked Jan 13 '10 09:01

Luc


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.

How do I add date and time picker?

If the Controls task pane is not visible, click More Controls on the Insert menu, or press ALT+I, C. Under Insert controls, click Date Picker. In the Date Picker Binding dialog box, select the field in which you want to store the date picker data, and then click OK.

How do I open calendar on android?

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.


2 Answers

Put both DatePicker and TimePicker in a layout XML.

date_time_picker.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:padding="8dp"     android:layout_height="match_parent">      <DatePicker         android:id="@+id/date_picker"         android:layout_width="match_parent"         android:calendarViewShown="true"         android:spinnersShown="false"         android:layout_weight="4"         android:layout_height="0dp" />      <TimePicker         android:id="@+id/time_picker"         android:layout_weight="4"         android:layout_width="match_parent"         android:layout_height="0dp" />      <Button         android:id="@+id/date_time_set"         android:layout_weight="1"         android:layout_width="match_parent"         android:text="Set"         android:layout_height="0dp" />  </LinearLayout> 

The code:

final View dialogView = View.inflate(activity, R.layout.date_time_picker, null); final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();  dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View view) {           DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);          TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);           Calendar calendar = new GregorianCalendar(datePicker.getYear(),                             datePicker.getMonth(),                             datePicker.getDayOfMonth(),                             timePicker.getCurrentHour(),                             timePicker.getCurrentMinute());           time = calendar.getTimeInMillis();          alertDialog.dismiss();     }}); alertDialog.setView(dialogView); alertDialog.show(); 
like image 153
Oded Breiner Avatar answered Oct 27 '22 04:10

Oded Breiner


Use this function it will enable you to pic date and time one by one then set it to global variable date. No library no XML.

Calendar date; public void showDateTimePicker() {    final Calendar currentDate = Calendar.getInstance();    date = Calendar.getInstance();    new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {        @Override        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {             date.set(year, monthOfYear, dayOfMonth);             new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {                 @Override                 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {                     date.set(Calendar.HOUR_OF_DAY, hourOfDay);                     date.set(Calendar.MINUTE, minute);                     Log.v(TAG, "The choosen one " + date.getTime());                 }             }, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), false).show();        }    }, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show(); } 
like image 26
Abhishek Avatar answered Oct 27 '22 06:10

Abhishek