Avoid Past Time in Time Picker in ANdroid
Suppose we have a task not to select past time with current time in ANdroid:
We use:
Suppose we have a task not to select past time with current time in ANdroid:
We use:
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @TargetApi(Build.VERSION_ CODES.N) @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())); } @TargetApi(Build.VERSION_ CODES.N) public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // Do something with the time chosen by the user CALENDER.set(Calendar.HOUR_OF_ DAY, hourOfDay); CALENDER.set(Calendar.MINUTE, minute); changeTime(); }
CHECK LOGIC HERE
@TargetApi(Build.VERSION_CODES.N) private void changeTime() { String myFormat = "HH:mm"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); Date currentTime = Calendar.getInstance(). getTime(); if(currentTime.compareTo(CALEN DER.getTime())<0) { BKD_ORD_TIME.setText(sdf. format(CALENDER.getTime())); } else{ Toast.makeText(getActivity(), "You Can't Set past time", Toast.LENGTH_SHORT).show(); } } } public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show( getSupportFragmentManager(), "timePicker"); }
Post a Comment
0Comments