Monday, September 2, 2013

Android Framework Bug with TimePicker Coupled with Dialog

The other day i found a bug that seemed unsolvable, relating to the fact that it was deep inside the android framework and the android operating system. That fact that using a timepicker inside a dialog and managing its lifecycle with "showDialog" cause the application to crash on an orientation change. This crash does not occur on any emulator.

Being aware of the fact that this problem was within the android framework and not in my code, i decided to dig around through the android framework source code, setting break points and trying to figure out what was going wrong, finally after a few days of digging, i figured out that the dialog and time picker both are trying to restore the time pickers state but the dialog(parent) actually assigns a blank value to the timepicker hence causing the exception seen. The exception is as follows:

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: setSpan (2 ... 2) ends beyond length 0
     at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:978)
     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:549)
     at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:541)
     at android.text.Selection.setSelection(Selection.java:74)
     at android.widget.EditText.setSelection(EditText.java:97)
     at android.widget.NumberPicker$SetSelectionCommand.run(NumberPicker.java:1632)
     at android.os.Handler.handleCallback(Handler.java:587)
     at android.os.Handler.dispatchMessage(Handler.java:92)
     at android.os.Looper.loop(Looper.java:132)
     at android.app.ActivityThread.main(ActivityThread.java:4028)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:491)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
     at dalvik.system.NativeStart.main(Native Method)

The solution was quite simple at the end when i understood what was causing the problem (it took quite some time to understand) just tell the timepickers parent not to manage the saved state, hence the following to lines were added under the fetching of the control.

//Line I mentioned
tp = (TimePicker) findViewById(R.id.timePickerComponent);
//two lines to add after
tp.setSaveFromParentEnabled(false);
tp.setSaveEnabled(true);
LESSON LEARNED : Its not impossible to move mountains.

Well comments and views are welcome, till next time! 

Tuesday, August 27, 2013

Time Picker Problem

An apparent discovery today being that, the android time picker actually doesnt update the time, when the "setIs24Hours" function is called. hence showing 10 am no matter if its 10 am or 10 pm when switching to 24hr clock.

The work around ive found for this problem, is quite a bit of common sense, but not very obvious, you have to set the time format to 24/12 hr clock before actually setting the time. This will avoid this problem.

BEST PRACTICE : Always do the settings of a object before assigning any values.

Well comments and views are welcome, till next time!

Sunday, August 11, 2013

Observation On Android OS About The Keyboard

The other day i encountered a baffling problem, where no matter what i did, the android keyboard wouldnt appear on activity startup, even though the editText was in focus, i even tried explicitly calling it, but no luck.

But heres the catch, when you wrap the focusable editTexts in a scrollView the keyboard shows up automatically, no need to call it at all. The scrollView collapses and the keyboard shows.

I tested a theory, and seems apparently true, though i could find no reference to it on the net, the android OS seems to block the keyboard from automatically being shown (even if explicitly called), if its going to show up over a focusable field (anything you can type in).

This makes sense since you would like to see what your typing when your typing it! Now for the reason it works with the scrollView, its quite simple, the andoird OS can keep the field in view by scrolling the view, so it isnt  a problem.

I havent found a way to override this feature, but the soultion seems viable and logical, wrapping activity components in a scrollview.

BEST PRACTICE : Have a scrollView at the activity base.

Well comments and views are welcome, till next time!

Device Information:

Thursday, August 8, 2013

The First Hello From Us!

Hello everyone, ive started these blogs to keep track of specific information for my self and any others wanting to know about some of the weird, unexpected and unorthodox things in the 3 platforms, java, c# and android.

Ill be posting from time to time when i find stuff, questions and comments are welcome!

Following other the links for all three blogs:
System.Out.println("Hello Code Gears"); (Java Blog)
Console.WriteLine("Hello Code Gears"); (C# Blog)
Log.d("Hello Code Gears"); (Android Blog)