How to programmatically scroll an HorizontalScrollView
I found that if you extend
the HorizontalScrollView
and override the onLayout
, you can cleanly scroll, after the super call to onLayout
:
MyScrollView extends HorizontalScrollView { protected void onLayout (boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); this.scrollTo(wherever, 0); }}
EDIT:For scrolling to start or end you can use:
this.fullScroll(HorizontalScrollView.FOCUS_RIGHT/FOCUS_LEFT);
instead of
this.scrollTo(wherever, 0);
public void autoSmoothScroll() { final HorizontalScrollView hsv = (HorizontalScrollView) view.findViewById(R.id.horiscroll); hsv.postDelayed(new Runnable() { @Override public void run() { //hsv.fullScroll(HorizontalScrollView.FOCUS_RIGHT); hsv.smoothScrollBy(500, 0); } },100); }
To test whether it's a timing issue (which I think it is), instead of calling scrollTo() in onStart, call postDelayed() with a Runnable that calls scrollTo, with a delay of 30 or so.