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.
The best Solution is to add focus options for the child of your scrollview :
Then your xml file will look like :
Just create an empty view at the top of linearlayout
Single line solves the problem
I made a test project to experiment with the various solutions if anyone wants to play with it. https://github.com/marchold/EditText-ErrorPopup-Scroll-View
Try this one :)
I had a slightly different objection to this infuriating deficiency. Whenever I tapped one of a number of RadioButtons below the EditTexts, the scroll position jumped to accommodate what Android determined to be the visible and focused EditText.
All attempts to retain the current desired scroll position via a
Runnable
that issuedScrollView.scrollTo(x,y)
were dutifully IGNORED by Android!I share my solution in the hope that it may save someone else 8 (eight) wasted hours.
The issue is not on the java code, but on the manifest code.
In your AndroidManifest.xml add an attribute to the Activity: