Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus randomly jumps when scrolling

I have an activity with many EditText controls and checkboxes near them. By default, most EditTexts are disabled.

When I open the activity, some random EditText control gets focus (a frame around it) and if you tap on it, the on-screen keyboard appears even though the EditText is disabled and no text appears when you press the on-screen keys.

Also, my whole layout is wrapped in a ScrollView. When you scroll, some random EditTexts get focus. It can be the lowest visible one, or the highest visible, or sometimes one in the middle, sometimes one outside the visible area.

Because a random element of the layout gets focus, the Activity gets randomly scrolled down when you open it, which is pretty annoying.

I guess it's an Android's bug, but is there a workaround?

Stop EditText from gaining focus at Activity startup handles the situation with only 1 EditText for which you can tell to lose focus so that the dummy element could gain it. In my case the dummy element doesn't gain focus, both in onResume or onCreate, with both android:focusable="true" android:focusableInTouchMode="true"

Should I check all the EditText controls (there are 12 of them) and tell them to lose focus? What with scrolling, because it seems focus randomly jumps.

like image 309
iseeall Avatar asked Jul 13 '11 13:07

iseeall


2 Answers

Because of the implementation of the fling Method in the ScrollView - it is sufficient to override the findFocus(), so that it will return this to prevent the focus from jumping around when scrolling.

@Override
public View findFocus() {
    return this;
}
like image 84
Skip Avatar answered Nov 17 '22 00:11

Skip


It's not scrolling that randomly focuses the EditText. It's when the scrollview handles a fling event. If you overwrite the fling method the won't be any random focus changes.

Then if you want the fling functionality back you'll need to write your own fling method. There's code here you can copy from:

Smooth scrolling in Android

like image 24
Li_W Avatar answered Nov 17 '22 02:11

Li_W