Stop ScrollView from auto-scrolling to an EditText

57,882

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.

Share:
57,882
Fraggle
Author by

Fraggle

Updated on September 24, 2020

Comments

  • Fraggle
    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 an EditText (or any view for that matter) that has focus.

    You have a bunch of views (Buttons, TextViews, 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.

  • Fraggle
    Fraggle almost 13 years
    This seems to be working, although leaves the button highlighted, I guess I can do a clearFocus() call right after the requestFocusFromTouch() call.
  • SaKet
    SaKet over 12 years
    I am saying that you need to explicitly have request focus on your views inside a scroll view in order to prevent auto scrolling
  • Mark
    Mark about 11 years
    This 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
    Softlion almost 11 years
    that 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
    Arun Jose over 10 years
    works-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
    Mark Freeman over 10 years
    This 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
    Mark Freeman over 10 years
    This was a good solution! It keeps the screen from having a visible focused view. Nice!
  • Cuong Thai
    Cuong Thai over 10 years
    If this still doesnt work, try this solution stackoverflow.com/questions/8100831/…
  • Diederik
    Diederik almost 10 years
    This 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
    Ruchira Randana over 8 years
    This is the best! Thanks.
  • Soo Chun Jung
    Soo Chun Jung about 6 years
    Thanks a lot! You save my day :)
  • Abed
    Abed over 2 years
    Great that's it I have been looking for a solution for ages for ScrollView's jump when text is selected.
  • Ahmed Shahid
    Ahmed Shahid about 2 years
    This way you are never able to focus on your EditText. When the user can't focus, they can't interact with it either.