Today I am going to explain Date and Time Picker in android.
In many applications you require to set date and time feature. Its very simple to implement in android.
Android provide default Date and Time picker widget. You just use it in xml and do the pre-define code for display it.
Sample Code:
1) For Date Picker:
File: MainActivity.java
public void populateSetDate(int year, int month, int day)
{
mEdit = (EditText)findViewById(R.id.editText1);
mEdit.setText(month+"/"+day+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd)
{
populateSetDate(yy, mm+1, dd);
}
}
Output:
Download Full Source Code: DatePicker
2) For Time Picker:
File: MainActivity.java
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute)
{
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
textViewTime.setText(new StringBuilder().append(padding_str(hour))..append(":") .append(padding_str(minute)));
// set current time into timepicker
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
}
};
private static String padding_str(int c)
{
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
Download Full Source Code: TimePicker
Enjoy with coding...!!!
In many applications you require to set date and time feature. Its very simple to implement in android.
Android provide default Date and Time picker widget. You just use it in xml and do the pre-define code for display it.
Sample Code:
1) For Date Picker:
File: MainActivity.java
public void populateSetDate(int year, int month, int day)
{
mEdit = (EditText)findViewById(R.id.editText1);
mEdit.setText(month+"/"+day+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd)
{
populateSetDate(yy, mm+1, dd);
}
}
Output:
Download Full Source Code: DatePicker
2) For Time Picker:
File: MainActivity.java
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute)
{
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
textViewTime.setText(new StringBuilder().append(padding_str(hour))..append(":") .append(padding_str(minute)));
// set current time into timepicker
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
}
};
private static String padding_str(int c)
{
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
Output:
Download Full Source Code: TimePicker
Enjoy with coding...!!!
No comments:
Post a Comment