92

Whenever I start this activity, it always starts bottomed out--scrolled all the way to the bottom. I am not doing anything weird in the activity OnCreate (or anywhere for that matter) that I would expect to change the scroll position. I have tried setting focus to the topmost focusable control, and the scrollto method, but neither of them are working. Besides, none of my other activities has this problem. Here's the layout:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_refresh_update_header"
            android:textSize="18sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Variable:"
            android:textSize="18sp"/>

        <Spinner
            android:id="@+id/edit_refresh_variables_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Network:"
            android:textSize="18sp"/>

        <RadioGroup
            android:id="@+id/widget1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical">

            <RadioButton
                android:text="Web"
                android:id="@+id/edit_refresh_update_rb_web"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="NetRadioButtonSelected"
                android:checked="true"/>

            <RadioButton
                android:text="Socket Server"
                android:id="@+id/edit_refresh_update_rb_socket_server"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="NetRadioButtonSelected"/>
        </RadioGroup>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Socket server request type:"
            android:textSize="18sp"/>

        <Spinner
            android:id="@+id/edit_refresh_socket_server_req_types_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Socket server body:"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/edit_refresh_update_ss_body"
            android:layout_height="wrap_content"
            android:enabled="false"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Url:"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/edit_refresh_update_url"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <Button
            android:text="Save refresh update"
            android:id="@+id/edit_refresh_save_btn"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginBottom="20dp"
            android:layout_alignParentBottom="true"
            android:onClick="SaveRefreshUpdate">
        </Button>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>
    </LinearLayout>
</ScrollView>
2
  • How exactly did you try to change focus? Mar 13, 2012 at 18:57
  • 1
    using the fulscroll method as described below and the scrollto method as also described below. I don't necessarily mind fixing it this way, but there must be a reason why it is starting like that and I'd rather fix it by correcting that.
    – MrGibbage
    Mar 13, 2012 at 19:08

12 Answers 12

310

When Android starts an activity, some control needs to take focus. When there's no designated control to take focus, the system chooses the first eligible control that wants focus. If you set the following property in your LinearLayout - android:focusableInTouchMode="true" your LinearLayout will be focused on start and your activity won't scroll to EditText in the bottom.

5
  • Not working in my GridLayout. I have a GridView in the middle of the layout, and it gets the focus every time.
    – Iqbal
    Jan 15, 2016 at 7:42
  • 2
    Scratched my head for a complete day and couldn't find as to why it was happening in just one particular activity and I still don't understand since I have not enabled focus on any view. But thanks to you, I managed to fix this issue. It'd be great if someone could throw some light as to what could be the possible reason since there is nothing new I'm doing in the new activity.
    – Shubhral
    Jun 13, 2016 at 6:36
  • 2
    An important note: this is to be added as property to the LinearLayout inside the ScrollView, not to the scroll view itself. May 8, 2017 at 7:26
  • @BorisStrandjev very important note. Should be added to the answer. By the way, thanks for that :P
    – Coreggon
    May 25, 2020 at 2:46
  • It is literally mentioned in the answer. May 26, 2020 at 12:47
35

Instead of polluting items in your layout, you can add this into your Activity/Class:

    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
    scrollView.setFocusableInTouchMode(true); 
    scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

This way it isn't an element within your scrollView getting focus, it's your scrollView as a container which get's the first focus, moving onto the child view's after this.

4
  • Partially works for me when I trying to set ListView height based on children inside ScrollView Mar 25, 2016 at 7:52
  • This helped! (while the same attributes in xml did not work..) Dec 9, 2017 at 11:26
  • Works like a charm! Thx
    – Bogy
    Aug 7, 2018 at 15:49
  • It did work for me, but I added this line: scrollView.fullScroll(ScrollView.FOCUS_UP) Both separately didn't work. May 31, 2019 at 17:33
4

You can try this code:

scroller.post(new Runnable() { 
    public void run() { 
        scroller.fullScroll(ScrollView.FOCUS_DOWN); 
    } 
}); 

[N:B] [referance link]1

1
  • 1
    its a good option but it simulates a jerk after opening so not a good user experience i think
    – iBabur
    May 31, 2016 at 21:01
3

Add these two lines in your ScrollView

 android:focusableInTouchMode="true"
 android:descendantFocusability="blocksDescendants"
0
2

Along with what devmiles.com said.

If you set the following property in your LinearLayout - android:focusableInTouchMode="true" your LinearLayout will be focused on start and your activity won't scroll to EditText in the bottom.

If you call requestLayout() on a view at the bottom of your linear layout it might steal the focus from your top most view. So just call requestFocus() on the top most view in the linear layout after calling requestLayout() on a lower view.

1

I don't know why but you could experiment with

ScrollView sv = (ScrollView) findViewById(R.id.scroll);
sv.scrollTo(0,0);

in your onCreate to manually scroll it to where you like it to be.

1
  • you saved me , everyone says ScrollView.FOCUS_UP but It has a side effect, if you are using edittext.setOnFocusChangeListener then it will get focus automatically to the edit text. Aug 6, 2021 at 14:30
1

Have you tried use fullScroll method ?http://developer.android.com/reference/android/widget/ScrollView.html#fullScroll(int)

yourScrollView.fullScroll(ScrollView.FOCUS_UP);
0
1

For Xamarin Android development, if you want to do @Graeme solution then:

// Set Focus to ScrollView
ScrollView scrollView = (ScrollView)FindViewById(Resource.Id.scrollView);
scrollView.FocusableInTouchMode = true;
scrollView.DescendantFocusability = Android.Views.DescendantFocusability.BeforeDescendants;
1

Just make it not focusable in onCreate()

editText.setFocusable(false);

and use

editText.setOnClickListener(new ....){
 ......
  editText.setFocusable(true);
  editText.setFocusableInTouchMode(true);
  editText.requestFocus();

  showSoftKeyboard(); //Use InputMethodManager to open soft Keyboard

 ......
 };

and it will not scroll down to Edittext next time, a fragment or activity is created.

0

scrollView.requestFocus(View.FOCUS_UP) solved my problem when I wanted the top view to be visible. Specifying direction parameter to the method requestFocus(int direction) is a good way to control the ScrollView position. More details here.

0

In my case I had a VideoView inside a ConstraintLayout inside a ScrollView. The VideoView was always stealing focus, and there are issues with it. Solution is to use a TextureView. Hopefully this helps someone, it cost me an hour at least :)

0

with Kotlin and ViewBinding u can add this in your fragment or activity :

binding.mainScroll.isFocusableInTouchMode = true
binding.mainScroll.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.