This post is regarding how to use/implement DateTimePicker in your android app.
DateTimePicker in android is a UI control which is very useful in your app and gives a nice feel to your app if your app contains some date or time related usage.
https://developer.android.com recommend that you use DialogFragment to host each time or date picker. The DialogFragment manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on your handsets or as an embedded part of the layout on large screens.
So let's start with the implementation of this UI control which is as simple as follows:
-Create a Time Picker
Create a static class to show your TimePicker by extending DialogFragment as follows:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hour, int minute) {
// Do something with the time chosen by the user
// Your selected time will be (hour:minute)
}
}
-Show the time picker
Now add a button to your layout xml file as follows:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_time"
android:onClick="showTimePickerDialog" />
-When you will click this button, the system will call the following method:
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
That's all. After this you will be able to see a TimePicker from where you can select your time. After selecting the time you can get your selected time (callback) in the onTimeSet() method of your TimePicker class that you just created above.
-Create a Date Picker
Create a static class to show your DatePicker by extending DialogFragment as follows:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
// Your selected date will be (day/month/year)
}
}
-Show the date picker
Now add another button to your layout xml file as follows:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
-When you will click this button, the system will call the following method:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
DateTimePicker in android is a UI control which is very useful in your app and gives a nice feel to your app if your app contains some date or time related usage.
https://developer.android.com recommend that you use DialogFragment to host each time or date picker. The DialogFragment manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on your handsets or as an embedded part of the layout on large screens.
So let's start with the implementation of this UI control which is as simple as follows:
-Create a Time Picker
Create a static class to show your TimePicker by extending DialogFragment as follows:
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hour, int minute) {
// Do something with the time chosen by the user
// Your selected time will be (hour:minute)
}
}
-Show the time picker
Now add a button to your layout xml file as follows:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_time"
android:onClick="showTimePickerDialog" />
-When you will click this button, the system will call the following method:
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
That's all. After this you will be able to see a TimePicker from where you can select your time. After selecting the time you can get your selected time (callback) in the onTimeSet() method of your TimePicker class that you just created above.
-Create a Date Picker
Create a static class to show your DatePicker by extending DialogFragment as follows:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
// Your selected date will be (day/month/year)
}
}
-Show the date picker
Now add another button to your layout xml file as follows:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
-When you will click this button, the system will call the following method:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
That's all. After this you will be able to see a DatePicker from where you can select your date. After selecting the date you can get your selected date (callback) in the onDateSet() method of your DatePicker class that you just created above.
Share and comment if any issues.
Share and comment if any issues.
No comments:
Post a Comment