Seems to be a common problem without a great solution that I have found. Goal is to stop a ScrollView
from auto-scrolling to an EditText
(or any view for that matter) that has focus.
You have a bunch of views (Button
s, TextView
s, etc) in an ScrollView
, one of which is an EditText
. Upon clicking say a Button within the ScrollView
, the ScrollView
scrolls down to the EditText
(its off screen). This is not desired, as there are other elements that you don't want scrolled off the screen.
Now I can stop this from happening when the screen first shows by having other focusable elements in the ScrollView
. However, the general problem still exists. The user scrolls down manually to the EditText
, enters some numbers, then scrolls up to the top (EditText
off screen now), they click a button in the ScrollView
, and guess what? The ScrollView
scrolls down to that darn EditText
.
I'm thinking about extending the ScrollView
and overriding some of the methods there like findFocusableViewInBounds
, but I have a feeling I'll just be getting myself into more trouble.
Please help if you can.
I've played around with things like having an 0 height EditText
at the top of my ScrollView
, adding Next Focusable element properties to the other items in the ScrollView
, etc. I suppose one "hack" might be to get the EditText
to lose focus when the virtual or manual keyboard gets hidden or something.
I was having a similar problem and finally got it to work. My scroll view contains a series of customized buttons, followed by an
EditText
(which normally has focus, but I don't want it to be losing focus). Any time the buttons were clicked, the scroll view auto-scrolled to the focusedEditText
. Overridingpublic boolean requestChildRectangleOnScreen(final View child, final Rect rectangle, final boolean immediate)
and always returningfalse
(default behavior of aViewGroup
) did the trick. Hope it helps with your situation too.By adding 2 parameters in:
In which Main layout is there.
By this
EditText
will not be auto focused.My fix to this most horrific bug, (worth noting that this is pre API11 only where they modified the fling method not to be stupid).
The old fling method finds the next focus that it will get to.. which isn't really that helpful. Other versions of this class don't really work as they stop focus working when the user genuinely traverses the form from the keyboard.
For me, it didn't work to override ScrollView onTouch. Also did not work android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" This and another mentioned solutions only worked for the first time - only when EditText is not selected, but once you select it, scrollview autoscrolls again.
Because I was already written a code to hide a keyboard when touching other views, I just added two lines of code and it worked like a champ:
also added this in root view:
Maybe its not really nice solution, but its working.
thomas88wp answer, https://stackoverflow.com/a/6486348/528746 worked for me. But I had two problems: 1. When scrolling, I wanted to hide the keyboard
2. I had lots of EditText views and didn't want to write it for each one of them
(I do getActivity() since I'm writing this inside a Fragment and not an activity)
Another version of thomas88wp's code: