TimePicker Dialog from clicking EditText
Solution 1
eReminderTime.setText( "" + selectedHour + ":" + selectedMinute);
Your missing a +
between ""
and selected hour, setText
methods only take a single string, so you need to concatenate all the parts (the first quotes are likely unnecessary).
eReminderTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
eReminderTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
That should fix your second error, you weren't providing the last parameter. TimePickerDialog Constructors
Solution 2
Why not write in a re-usable way ?
Create SetTime class:
class SetTime implements OnFocusChangeListener, OnTimeSetListener {
private EditText editText;
private Calendar myCalendar;
public SetTime(EditText editText, Context ctx){
this.editText = editText;
this.editText.setOnFocusChangeListener(this);
this.myCalendar = Calendar.getInstance();
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
int hour = myCalendar.get(Calendar.HOUR_OF_DAY);
int minute = myCalendar.get(Calendar.MINUTE);
new TimePickerDialog(ctx, this, hour, minute, true).show();
}
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
this.editText.setText( hourOfDay + ":" + minute);
}
}
Then call it from onCreate function:
EditText editTextFromTime = (EditText) findViewById(R.id.editTextFromTime);
SetTime fromTime = new SetTime(editTextFromTime, this);
Solution 3
You can use the below code in the onclick listener of edittext
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
tv_time.setText(hourOfDay + ":" + minute);
}
}, hour, minute, false);
timePickerDialog.show();
You can see the full code at Android timepicker example
Solution 4
I extended the nice reusable solution of @Sumoanand to support both focus change and click listeners when changing the time multiple times. Also updating the picker calendar to remember the last selected time + formatting HH:mm
public class TimeSetter implements View.OnFocusChangeListener, TimePickerDialog.OnTimeSetListener, View.OnClickListener {
private EditText mEditText;
private Calendar mCalendar;
private SimpleDateFormat mFormat;
public TimeSetter(EditText editText){
this.mEditText = editText;
mEditText.setOnFocusChangeListener(this);
mEditText.setOnClickListener(this);
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
showPicker(view);
}
}
@Override
public void onClick(View view) {
showPicker(view);
}
private void showPicker(View view) {
if (mCalendar == null)
mCalendar = Calendar.getInstance();
int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
int minute = mCalendar.get(Calendar.MINUTE);
new TimePickerDialog(view.getContext(), this, hour, minute, true).show();
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
if (mFormat == null)
mFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
this.mEditText.setText(mFormat.format(mCalendar.getTime()));
}
}
Usage from onCreate:
EditText timeEditText = (EditText) rootView.findViewById(R.id.timeText);
new TimeSetter(timeEditText);
Solution 5
I dont know if this will work for you, it works for me just fine.
Create a method for the Date/Time picker dialog.
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is called, below method will be called.
// The arguments will be working to get the Day of Week to show it in a special TextView for it.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
String year1 = String.valueOf(selectedYear);
String month1 = String.valueOf(selectedMonth + 1);
String day1 = String.valueOf(selectedDay);
delivDate.setText(month1 + "/" + day1 + "/" + year1);
delivDay.setText(DateFormat.format("EEEE", new Date(selectedYear, selectedMonth, selectedDay - 1)).toString());
}
};
and then, wherever you want you can do it just like this
public void setDateOnClick (View view) {
Calendar cal = Calendar.getInstance();
DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener,
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
//Create a cancel button and set the title of the dialog.
datePicker.setCancelable(false);
datePicker.setTitle("Select the date");
datePicker.show();
}
hope you find this as your solution.
![Mark O'Sullivan](https://i.stack.imgur.com/PuTWR.jpg?s=256&g=1)
Comments
-
Mark O'Sullivan over 3 years
I've already got a DatePicker which pops up when the user clicks on the EditText field
eReminderDate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //To show current date in the datepicker Calendar mcurrentDate = Calendar.getInstance(); int mYear = mcurrentDate.get(Calendar.YEAR); int mMonth = mcurrentDate.get(Calendar.MONTH); int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH); DatePickerDialog mDatePicker; mDatePicker = new DatePickerDialog(AddReminder.this, new OnDateSetListener() { public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) { // TODO Auto-generated method stub /* Your code to get date and time */ selectedmonth = selectedmonth + 1; eReminderDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear); } }, mYear, mMonth, mDay); mDatePicker.setTitle("Select Date"); mDatePicker.show(); } });
I've tried doing a TimePicker in a similar way but was unable to get it working. This is my attempt at getting it working
eReminderTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Calendar mcurrentTime = Calendar.getInstance(); int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); int minute = mcurrentTime.get(Calendar.MINUTE); TimePickerDialog mTimePicker; mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { eReminderTime.setText( ""selectedHour + ":" + selectedMinute); } }, hour, minute); mTimePicker.setTitle("Select Time"); mTimePicker.show(); } });
Is it impossible to do it similar to the way I did it for DatePicker?
I've tried even just making a TimePicker pop up once the EditText field is clicked with this code.
eReminderTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(TIME_DIALOG_ID); } });
For some reason when I entered that into Android Studio the 'showDialog' was scored out.
Can anyone give me tips on where I'm going wrong? Or am I just going to have to use a Custom DialogFragment?
-
Mark O'Sullivan almost 11 yearsTried that and think I might of tried that before because I got this message before. Is now showing an error everything after 'new TimePickerDialog' and before 'mTimePicker.setTitle'. The error is 'Cannot resolve constructor 'TimePickerDialog(com.appname.classname, android.app.TimePickerDialog.OnTimeSetListener, int, int)' '
-
Robadob almost 11 yearsIf you check the android documentation, you need to add a final boolean parameter stating whether the dialog is 24 hours or not. TimePickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView)
-
Mark O'Sullivan almost 11 yearsCompletely forgot about the true/ false boolean, seen that earlier but forgot to include it when I changed my code to something similar to the date picker. Thanks for your help!
-
Stephen Paul over 9 yearsAwesome thanks. You're probably going to want to format the minutes too: eReminderTime.setText( selectedHour + ":" + String.format("%02d", selectedMinute));
-
MojioMS about 8 yearsI like the two lines "// Create a cancel button [...]" and "datePicker.setCancelable(false);" hue hue hue
-
Ar5hv1r about 8 yearsPlease don't just post code; explain what your code is doing and why it resolves OP's problem.
-
Pyves almost 7 yearsSome additional information about this code only answer would probably be beneficial.
-
Pratik Butani over 5 yearsHow to get AM/PM?
-
Pratik Butani over 5 yearsI think this question is not related to the question.
-
Pratik Butani over 5 yearsThank you for your solution :) I have edited it to use with AM/PM. Check out here : TimePicker with/without AM/PM
-
Pratik Butani over 5 yearsI hope it will be more useful and re-usable -> TimePickerUniversal
-
alierdogan7 about 3 years
eReminderTime .setText(String.format("%02d:%02d", selectedHour, selectedMinute))
produces better time representation.