On Android I set an EditText with a TimePicker Dialog. Default Time is now, but using TimePicker time shuld change.
I have three issues:
1) Selecting another time, sometimes displayng time on EditText not change;
2) If I tip on TimePicker and choose the same time, it change of +12h ( for example now is 09.35 but it display 21.35 );
3) If I choose an hour >= 17.00 I got an exception:
07-19 11:47:17.574 16224-16224/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapp, PID: 16224
java.lang.ArrayIndexOutOfBoundsException: length=17; index=18
at java.util.Calendar.set(Calendar.java:1122)
at com.example.myapp.activities.MyActivity$9.onTimeSet(MyActivity.java:513)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:133)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
This is my TimePicker function:
// TimePicker settings
public void setTimeFields(final EditText editText) {
// Calendar instance
final Calendar calendar = Calendar.getInstance();
// TimePicker definition
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(hourOfDay, minute);
editText.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(calendar.getTime()));
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(this));
// Show dialog
timePickerDialog.show();
}
And here my editext definition:
etxTime = (EditText) findViewById(R.id.etxTime);
etxTime.setInputType(InputType.TYPE_NULL);
etxTime.setText(new SimpleDateFormat("HH:mm", Locale.ITALIAN).format(Calendar.getInstance().getTime()));
etxTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setTimeFields(etxTime); } });
Where I'm wrong?
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Show Date dialog
showDialog(Date_id);
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Show time dialog
showDialog(Time_id);
}
});
}
protected Dialog onCreateDialog(int id) {
// Get the calander
Calendar c = Calendar.getInstance();
// From calander get the year, month, day, hour, minute
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
switch (id) {
case Date_id:
// Open the datepicker dialog
return new DatePickerDialog(MainActivity.this, date_listener, year,
month, day);
case Time_id:
// Open the timepicker dialog
return new TimePickerDialog(MainActivity.this, time_listener, hour,
minute, false);
}
return null;
}
// Date picker dialog
DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// store the data in one string and set it to text
String date1 = String.valueOf(month) + "/" + String.valueOf(day)
+ "/" + String.valueOf(year);
set_date.setText(date1);
}
};
TimePickerDialog.OnTimeSetListener time_listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour, int minute) {
// store the data in one string and set it to text
String time1 = String.valueOf(hour) + ":" + String.valueOf(minute);
set_time.setText(time1);
}
};
where "date" , "time" are buttons and "set_date", "set_time" are editTexts in this case.