How to add android date time picker?

Android Date Time Picker Dialog.

Add the following namespace.

->Android additionally supports widgets such as DatePicker, TimePicker as well as dialogs just like DatePickerDialog, TimePickerDialog for helping users enter dates and times.
->The DatePicker and also DatePickerDialog are same it’s enable you to set the starting date for selection, in the form of a year, month, and day of month value.
->Both are give a callback object OnDateChangedListener or even OnDateSetListener where you are informed of a new date selected from the user.
->You are able to store that date somewhere because there is no some other way for you to get at the chosen date later on.
->To Test this Date and Time Picker widget, create an android project and place two buttons.One for set the date and another one for set the time. And display the end result in a textview.

Demo example of Date Time picker is like below:

Creating Layout for Date Time Picker Widget


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/timeTxt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/time_text" />
    <Button
        android:id="@+id/dateBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/set_date"
        android:onClick="chooseDate"
        />
    <Button
        android:id="@+id/timeBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/set_time"
        android:onClick="chooseTime"
        />
</LinearLayout>

Java code to configure the Date Time Picker Widget:


AndroidDateTimePickerActivity.java

package com.vimaltuts.android.androiddatetimepicker;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import java.text.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class AndroidDateTimePickerActivity extends Activity {
private Button timeBtn;
private Button dateBtn;
DateFormat formatDateTime=DateFormat.getDateTimeInstance();
Calendar dateTime=Calendar.getInstance();
private TextView timeLabel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timeLabel=(TextView)findViewById(R.id.timeTxt);
updateLabel();
}
public void chooseDate(){
new DatePickerDialog(AndroidDateTimePickerActivity.this, d, dateTime.get(Calendar.YEAR),dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH)).show();
}
public void chooseTime(){
new TimePickerDialog(AndroidDateTimePickerActivity.this, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();
}
DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
dateTime.set(Calendar.YEAR,year);
dateTime.set(Calendar.MONTH, monthOfYear);
dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
TimePickerDialog.OnTimeSetListener t=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateTime.set(Calendar.MINUTE,minute);
updateLabel();
}
};
private void updateLabel() {
timeLabel.setText(formatDateTime.format(dateTime.getTime()));
}
}
->In the button property we allocated a related method to each button that should be called when the user clicks it.
->For the DatePickerDialog, we give it a OnDateSetListener callback that updates the Calendar with the new date.
->For the TimePickerDialog, it gets a OnTimeSetListener callback to update the time part of the Calendar.

For more detail Click here..

About the Author

avatar