Stop ScrollView from auto-scrolling to an EditText
Solution 1
After struggling with that problem for quite some time, I've found a solution that seems to work without being too ugly. First, make sure that whatever ViewGroup
(directly) contains your EditText
has descendantFocusability
set to "Before Descendants," focusable
set to "true" and focusableInTouchMode
set to "true." This will not be the ScrollView
itself, but the layout inside where you have your various views. Next add an onTouchListener
to your ScrollView
that removes focus from the EditText
whenever it is touched, like so:
ScrollView scroll = (ScrollView)findViewById(R.id.scrollView1);
scroll.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (myEditText.hasFocus()) {
myEditText.clearFocus();
}
return false;
}
});
Tell me if that doesn't fix it. What should happen is that the Layout gets focus instead of the EditText
, so no scrolling should happen.
Solution 2
Just create an empty view at the top of linearlayout
<View android:layout_width="fill_parent" android:id="@+id/focus_view" android:layout_height="0dp" android:focusable="true" android:focusableInTouchMode="true"><requestFocus/></View>
Single line solves the problem
Solution 3
I had the same problem. There's one trick that I'm using to deal with this problem:
public void onClick(View v) {
button.requestFocusFromTouch(); //prevents from loosing focus and scrolling view down
....
}
Solution 4
The issue is not on the java code, but on the manifest code.
In your AndroidManifest.xml add an attribute to the Activity:
<activity android:name=".MyActivity" android:windowSoftInputMode="adjustPan"> </activity>
Solution 5
By adding 2 parameters in:
android:focusable="true"
android:focusableInTouchMode="true"
In which Main layout is there.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:focusable="true"
android:focusableInTouchMode="true">
By this EditText
will not be auto focused.
Fraggle
Updated on September 24, 2020Comments
-
Fraggle over 3 years
Seems to be a common problem without a great solution that I have found. Goal is to stop a
ScrollView
from auto-scrolling to anEditText
(or any view for that matter) that has focus.You have a bunch of views (
Button
s,TextView
s, etc) in anScrollView
, one of which is anEditText
. Upon clicking say a Button within theScrollView
, theScrollView
scrolls down to theEditText
(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 theEditText
, enters some numbers, then scrolls up to the top (EditText
off screen now), they click a button in theScrollView
, and guess what? TheScrollView
scrolls down to that darnEditText
.I'm thinking about extending the
ScrollView
and overriding some of the methods there likefindFocusableViewInBounds
, 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 myScrollView
, adding Next Focusable element properties to the other items in theScrollView
, etc. I suppose one "hack" might be to get theEditText
to lose focus when the virtual or manual keyboard gets hidden or something. -
Fraggle almost 13 yearsThis seems to be working, although leaves the button highlighted, I guess I can do a clearFocus() call right after the requestFocusFromTouch() call.
-
SaKet over 12 yearsI am saying that you need to explicitly have request focus on your views inside a scroll view in order to prevent auto scrolling
-
Mark about 11 yearsThis is the best 1 liner to solve the problem. I wish a mod would delete the rest. This must be exactly what he was looking for. It worked great for me
<requestFocus />
after the ending bracket of whatever you want to have focus when the page loads. -
Softlion almost 11 yearsthat does work, BUT requires focusableInToucheMode, which have a big drawback: you have to tap twice the control to trigger it ... so not good solution ...
-
Arun Jose over 10 yearsworks-horizontal scrollview, editing the last cell in the table row, the position is maintained.. thanks!! requires, android:focusable="true" android: focusableInToucheMode="true" and <requestFocus></requestFocus>
-
Mark Freeman over 10 yearsThis is a cleaner implementation than above. thank you. One note is that if using fragments, you would want to get the rootView from the fragment, rather than attempting to get the scrollview from the activity.
-
Mark Freeman over 10 yearsThis was a good solution! It keeps the screen from having a visible focused view. Nice!
-
Cuong Thai over 10 yearsIf this still doesnt work, try this solution stackoverflow.com/questions/8100831/…
-
Diederik almost 10 yearsThis worked when the
ScrollView
is in an activity. When it contains a fragment that needs to scroll I used the drop focus on scroll solution. -
Ruchira Randana over 8 yearsThis is the best! Thanks.
-
Soo Chun Jung about 6 yearsThanks a lot! You save my day :)
-
Abed over 2 yearsGreat that's it I have been looking for a solution for ages for ScrollView's jump when text is selected.
-
Ahmed Shahid about 2 yearsThis way you are never able to focus on your EditText. When the user can't focus, they can't interact with it either.