How to scroll to top of long ScrollView layout?

AndroidListviewScrollview

Android Problem Overview


For part of my app, the user is presented with a list of names and is asked to group them as they see fit.

(Note, the ListView code was copied verbatim from the Android Views tutorial. I haven't yet customized it to my needs, I'm just trying to figure out how to make this work.)

The basic layout is a LinearLayout, containing a ScrollView (called "groupsScrollView" in the code below), containing a RelativeLayout. I have some buttons and text, and then my ListView beneath that, which displays the list of names. All this is taller than the visible screen area, so the user is allowed to scroll vertically to see it all.

This all works beautifully, except when the page loads it is always pre-scrolled to the top of my ListView - in the middle of the page. The text and buttons I've created that tell the user what to do are not visible.

I can grab the screen and scroll up, that works just fine, but I'd like the screen to load having already been scrolled to the top. The user shouldn't have to scroll UP to see the top of a freshly loaded page. But everything I've tried to programmatically scroll to the top of the screen has failed.

Here's my onCreate method:

protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	
	setContentView(R.layout.groups);

	mainScrollView = (ScrollView)findViewById(R.id.groupsScrollView);
	
	//get the Bundle out of the Intent...
    Bundle extras = getIntent().getExtras();
    mNames = extras.getStringArray("mNames");
	
	setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mNames));

	ListView lv = getListView();
	lv.setTextFilterEnabled(true);

    //This is the line I'm having issues with
	mainScrollView.pageScroll(View.FOCUS_UP);
	
	lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	      // When clicked, show a toast with the TextView text
	      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
	          Toast.LENGTH_SHORT).show();
	    }
	  });
}

The line "mainScrollView.pageScroll(View.FOCUS_UP); is the problem. It doesn't cause an error, but it doesn't seem to do anything. I've tried scrollTo(0,0), scrollDirection(View.FOCUS_UP), and everything else I can think of.

Since I don't get an error I have to assume that these scroll commands are actually working, but that they're scrolling to the top of the ListView, rather than the top of the ScrollView or RelativeLayout that contains it. This seems to be confirmed by Google's own description of the scrollTo(int x, int y) method where they say "This version also clamps the scrolling to the bounds of our child.".

So to make a long question even longer, how do I load a bunch of views on the screen contained within a ScrollView, and then programmatically scroll the whole thing to the top of the page so the user can interact with it?

Thanks!

Android Solutions


Solution 1 - Android

Try

mainScrollView.fullScroll(ScrollView.FOCUS_UP);

it should work.

Solution 2 - Android

Had the same issue, probably some kind of bug.

Even the fullScroll(ScrollView.FOCUS_UP) from the other answer didn't work.
Only thing that worked for me was calling scroll_view.smoothScrollTo(0,0) right after the dialog is shown.

Solution 3 - Android

i had the same problem and this fixed it. Hope it helps you.

listView.setFocusable(false);

Solution 4 - Android

scrollViewObject.fullScroll(ScrollView.FOCUS_UP) this works fine, but only the problem with this line is that, when data is populating in scrollViewObject, has been called immediately. You have to wait for some milliseconds until data is populated. Try this code:

scrollViewObject.postDelayed(new Runnable() {
    @Override
    public void run() {
        scroll.fullScroll(ScrollView.FOCUS_UP);
    }
}, 600);

OR

 scrollViewObject.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        scrollViewObject.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        scrollViewObject.fullScroll(View.FOCUS_UP);
    }
});

Solution 5 - Android

The main problem to all these valid solutions is that you are trying to move up the scroll when it isn't drawn on screen yet.

You need to wait until scroll view is on screen, then move it to up with any of these solutions. This is better solution than a postdelay, because you don't mind about the delay.

	// Wait until my scrollView is ready
	scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
		@Override
		public void onGlobalLayout() {
			// Ready, move up
			scrollView.fullScroll(View.FOCUS_UP);
		}
	});

Solution 6 - Android

Very easy

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

Solution 7 - Android

I faced Same Problem When i am using Scrollview inside View Flipper or Dialog that case scrollViewObject.fullScroll(ScrollView.FOCUS_UP) returns false so that case scrollViewObject.smoothScrollTo(0, 0) is Worked for me

Scroll Focus Top

Solution 8 - Android

 final ScrollView scrollview = (ScrollView)findViewById(R.id.selected_place_layout_scrollview);
         
         scrollview.post(new Runnable() { 
        	    public void run() { 
        	        scrollview.scrollTo(0, 0); 
        	    } 
        	}); 

Solution 9 - Android

This is worked for me

scroll_view.smoothScrollTo(0,0); // scroll to top of screen

Solution 10 - Android

runOnUiThread( new Runnable(){
   @Override
   public void run(){
      mainScrollView.fullScroll(ScrollView.FOCUS_UP);
   }
}

Solution 11 - Android

I fixed my issue in Kotlin like this:

scrollview.isFocusableInTouchMode = true
scrollview.fullScroll(View.FOCUS_UP)
scrollview.smoothScrollTo(0,0)

You can also create an extension of this like:

ScrollView.moveToTop()
{
    this.isFocusableInTouchMode = true
    this.fullScroll(View.FOCUS_UP)
    this.smoothScrollTo(0,0)
}

and use it like:

scrollView.moveToTop()

Solution 12 - Android

This is force scroll for scrollview :

            scrollView.post(new Runnable() {
                @Override
                public void run() {
                    scrollView.scrollTo(0, 0);
                    scrollView.pageScroll(View.FOCUS_UP);
                    scrollView.smoothScrollTo(0,0);
                }
            });

Solution 13 - Android

scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        // Ready, move up
        scrollView.fullScroll(View.FOCUS_UP);
    }
});

Solution 14 - Android

This solution also works for NestedScrollView

NestedScrollView nestedScrollView = view.findViewById(R.id.YourNestedScrollViewID);
nestedScrollView.fullScroll(ScrollView.FOCUS_UP);

Solution 15 - Android

@SuppressWarnings({ "deprecation", "unchecked" })
public void swipeTopToBottom(AppiumDriver<MobileElement> driver) 
                    throws InterruptedException {
       Dimension dimensions = driver.manage().window().getSize();
		Double screenHeightStart = dimensions.getHeight() * 0.30;
		int scrollStart = screenHeightStart.intValue();
		System.out.println("s="+scrollStart);
		Double screenHeightEnd = dimensions.getHeight()*0.90;
		int scrollEnd = screenHeightEnd.intValue();
		driver.swipe(0,scrollStart,0,scrollEnd,2000);
		CommonUtils.threadWait(driver, 3000);
	}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlennView Question on Stackoverflow
Solution 1 - AndroidroomtekView Answer on Stackoverflow
Solution 2 - AndroidMohamed HafezView Answer on Stackoverflow
Solution 3 - AndroidMiguel PalaciosView Answer on Stackoverflow
Solution 4 - AndroidMohammad ImranView Answer on Stackoverflow
Solution 5 - AndroidPelanesView Answer on Stackoverflow
Solution 6 - AndroidCommonSenseCodeView Answer on Stackoverflow
Solution 7 - AndroidsravanView Answer on Stackoverflow
Solution 8 - AndroidMladen RakonjacView Answer on Stackoverflow
Solution 9 - AndroidPankaj TalaviyaView Answer on Stackoverflow
Solution 10 - AndroidtraeperView Answer on Stackoverflow
Solution 11 - AndroidMaKiView Answer on Stackoverflow
Solution 12 - AndroidHadi NoteView Answer on Stackoverflow
Solution 13 - Androiduser9271802View Answer on Stackoverflow
Solution 14 - AndroidtoskanView Answer on Stackoverflow
Solution 15 - AndroidAjeet GourView Answer on Stackoverflow