google

Custom Date Time Picker for Android

| Friday, November 6, 2015
Android itself does not provide any date or time picker which can let user choose date and time together.

We can choose dates using DatePickerDialog and times using TimePickerDialog, which requires 2 different dialogs and 2 different actions. Instead of that we can use following code to provide selection of Date and Time in one dialog.

Following is the code of the layout file "date_time_picker.xml" which will be inflated to be displayed in the Date Time Picker Dialog.


 <?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="10dp"  
   android:layout_height="match_parent">  
   <DatePicker  
     android:id="@+id/datepicker"  
     android:layout_width="match_parent"  
     android:calendarViewShown="true"  
     android:spinnersShown="false"  
     android:layout_weight="4"  
     android:layout_height="0dp" />  
   <TimePicker  
     android:id="@+id/timepicker"  
     android:layout_weight="4"  
     android:layout_width="match_parent"  
     android:layout_height="0dp" />  
   <Button  
     android:id="@+id/datetimeset"  
     android:layout_weight="1"  
     android:layout_width="match_parent"  
     android:text="Set"  
     android:layout_height="0dp" />  
 </LinearLayout>  

Following is the code of Button click in Java file which will open the dialog.


  public void SelectDateTime(View v) {  
     final View dialogView = View.inflate(this, R.layout.date_time_picker, null);  
     final AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
     dialogView.findViewById(R.id.datetimeset).setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datepicker);  
         TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.timepicker);  
         Calendar calendar = new GregorianCalendar(datePicker.getYear(),  
             datePicker.getMonth(),  
             datePicker.getDayOfMonth(),  
             timePicker.getCurrentHour(),  
             timePicker.getCurrentMinute());  
         SimpleDateFormat mSDF = new SimpleDateFormat("HH:mm:ss");  
         String time = mSDF.format(calendar.getTime());  
         int day = datePicker.getDayOfMonth();  
         int month = datePicker.getMonth();  
         int year = datePicker.getYear();  
         SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");  
         String formatedDate = sdf.format(new Date(year-1900, month, day));  
         editText_datetime.setText(formatedDate + ' ' + time);  
         alertDialog.dismiss();  
       }  
     });  
     alertDialog.setView(dialogView);  
     alertDialog.show();  
   }  


"SelectDateTime" method is called on button click which will fetch the values from Date Time picker into the EditText.

"SimpleDateFormat" class has been used to set the values in desired date and time formats.

Important

String formatedDate = sdf.format(new Date(year-1900, month, day));  

Always +/- 1900 with year formatted using SimpleDateFormat. 

Custom Date Time Picker

2 comments:

  1. Asking questions are genuinely pleasant thing if you are not understanding something entirely, except this piece of writing presents nice understanding yet. facebook log in facebook

    ReplyDelete

Next Prev
▲Top▲