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?
Why not write in a re-usable way ?
Create SetTime class:
Then call it from onCreate function:
You can use the below code in the onclick listener of edittext
You can see the full code at Android timepicker example
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).That should fix your second error, you weren't providing the last parameter. TimePickerDialog Constructors
I dont know if this will work for you, it works for me just fine.
Create a method for the Date/Time picker dialog.
and then, wherever you want you can do it just like this
hope you find this as your solution.