5

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.

Community
  • 1
  • 1
iseeall
  • 3,381
  • 9
  • 34
  • 43
  • Just tried calling clearFocus() and setSelected(false) on every EditText element in the layout. This didn't help. Still some random one gets focus. – iseeall Jul 13 '11 at 14:06
  • After some testing on this found out that it's not always the random one getting focus, but the EditText element which has the same y-position as the point of touch. You don't have to touch exactly the element itself, but to the right or left of it and then start scrolling. After some time the EditText has a high chance to get focus, even if it's disabled (not editable) – iseeall Jul 14 '11 at 16:40

2 Answers2

4

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;
}
Skip
  • 6,240
  • 11
  • 67
  • 117
  • How do you do this without subclassing ScrollView? – SoloPilot Jan 20 '14 at 15:24
  • impossible. you will have to subclass `ScrollView` – Skip Jan 20 '14 at 15:42
  • Subclassing ScrollView didn't work for my problem. Can you help? I just posted my question at http://stackoverflow.com/questions/21240063/android-edittext-focus-jumps-to-another-inside-srollview – SoloPilot Jan 20 '14 at 20:10
3

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

Community
  • 1
  • 1
Li_W
  • 759
  • 1
  • 5
  • 15
  • i am facing the same focus jumping problem. but i am unable to override the Smooth scrolling in Android code. Can you please give me some more details to resolve this problem. Thanks in advance. – sathish Jan 03 '12 at 07:32