Created
February 18, 2014 07:34
-
-
Save akbarsha03/9066203 to your computer and use it in GitHub Desktop.
Use TimePickerDialog to get time from the user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use a `TimePickerDialog` to get the `runDelay` from user to do so | |
// **class variable:** | |
private TimePickerDialog time_picker; | |
private Calendar targetCal; | |
// **A class constructor** | |
public YOurClassName() { | |
final Calendar c = Calendar.getInstance(); | |
mYear = c.get(Calendar.YEAR); | |
mMonth = c.get(Calendar.MONTH); | |
mDay = c.get(Calendar.DAY_OF_MONTH); | |
mHour = c.get(Calendar.HOUR_OF_DAY); | |
mMinute = c.get(Calendar.MINUTE); | |
} | |
// **inside onCreate() or onActivityCreated():** | |
time_picker = new TimePickerDialog(getActivity(), mTimeSetListener, | |
mHour, mMinute, false); | |
// **Inside the class:** | |
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { | |
// the callback received when the user "sets" the TimePickerDialog in | |
// the dialog | |
public void onTimeSet(TimePicker view, int hourOfDay, int min) { | |
hour = hourOfDay; | |
minute = min; | |
Calendar calNow = Calendar.getInstance(); | |
Calendar calSet = (Calendar) calNow.clone(); | |
calSet.set(Calendar.HOUR_OF_DAY, hour); | |
calSet.set(Calendar.MINUTE, minute); | |
calSet.set(Calendar.SECOND, 0); | |
calSet.set(Calendar.MILLISECOND, 0); | |
if (calSet.compareTo(calNow) <= 0) { | |
// Today Set time passed, count to tomorrow | |
calSet.add(Calendar.DATE, 1); | |
} | |
targetCal = calSet; | |
} | |
}; | |
// use the `targetCal` in your code like this | |
alarm.set(AlarmManager.RTC, targetCal.getTimeInMillis(), pending_intent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment