Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid TimePickerDialog - Only works half the time (Literally)
    primarykey
    data
    text
    <p>Mac OS-X</p> <p>ECLIPSE with ADT</p> <p>ANDROID</p> <p>................</p> <p>Problem: When I pick the time, say 06:00, it sets the time as 18:00, when I pick the time 18:00 it sets the time as 06:00. I can't seem to figure out why. </p> <p>................</p> <p>LOGIC</p> <p>Gets current date instance. </p> <p>OnClickListener is set to a ButtonImage.</p> <p>OnClick calls showTimeDialog with 2 arguments (a TextView, the current date instance).</p> <p>TimePicker Dialog shows with at a set time of 09:00.</p> <p>TimeSet sets the time of a Calendar activeTime and updates a TextView</p> <p>..............</p> <p>ADDITIONAL NEW INFO</p> <p>Ok, so strange thing happened, now my code seems to work, BUT last time I checked it was before 24:00 and it was acting as described. It works now and the time is 00:20 here in the UK. I dont know if this has anything to do with it but I will update this post if anything changes. </p> <p>ADDITIONAL NEW INFO 2</p> <p>My code seems to work between 00:00 - 12:00 but from 12:00 - 24:00 it doesn't. I'm stumped..</p> <p>.............</p> <p>Here is my code:</p> <pre><code> /////// SETUP TIME // capture our View elements for the start time function startTimeDisplay = (TextView) findViewById(R.id.editText2); endTimeDisplay = (TextView) findViewById(R.id.editText3); startPickTime = (ImageButton) findViewById(R.id.imageButtonStartTime); endPickTime = (ImageButton) findViewById(R.id.imageButtonEndTime); // Use the current time as the default values for the time picker startTime = Calendar.getInstance(); endTime = Calendar.getInstance(); startTime.get(Calendar.DATE); endTime.get(Calendar.DATE); // add a click listener to the button startPickTime.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showTimeDialog(startTimeDisplay, startTime); } }); /* add a click listener to the button */ endPickTime.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showTimeDialog(endTimeDisplay, endTime); } }); } @Override protected Dialog onCreateDialog(int id) { getTime(activeTime); int hr = activeTime.get(Calendar.HOUR_OF_DAY); int mn = activeTime.get(Calendar.MINUTE); Log.d(TAG, "first hr..." +hr); Log.d(TAG, "first mn..." +mn); switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); case TIME_DIALOG_ID: return new TimePickerDialog(this, timeSetListener, THE_HOUR, THE_MINUTE, true); } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); getTime(activeTime); int hr = activeTime.get(Calendar.HOUR_OF_DAY); int mn = activeTime.get(Calendar.MINUTE); Log.d(TAG, "second hr..." +hr); Log.d(TAG, "second mn..." +mn); switch (id) { case DATE_DIALOG_ID: ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH)); break; case TIME_DIALOG_ID: ((TimePickerDialog) dialog).updateTime(THE_HOUR, THE_MINUTE); break; } } public void showTimeDialog(TextView timeDisplay, Calendar date) { activeTimeDisplay = timeDisplay; activeTime = date; showDialog(TIME_DIALOG_ID); } private OnTimeSetListener timeSetListener = new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hour, int minute) { activeTime.set(Calendar.HOUR, hour); activeTime.set(Calendar.MINUTE, minute); updateTimeDisplay(activeTimeDisplay, activeTime); unregisterTimeDisplay(); } }; private void updateTimeDisplay(TextView timeDisplay, Calendar date) { //convert Calendar to Unix Time long activeTimeUnixTime = (long) (date.getTimeInMillis()); //convert Unix Time to Date java.util.Date startTimeDate = new java.util.Date(activeTimeUnixTime); //convert Date to SimpleDateFormat and convert to String SimpleDateFormat formatter2; formatter2 = new SimpleDateFormat("kk:mm", Locale.UK); String stringTime = formatter2.format(startTimeDate); timeDisplay.setText(stringTime); } private void unregisterTimeDisplay() { activeTimeDisplay = null; activeTime = null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload