1

Why does doing

HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.buttons);
if (null != hsv) hsv.scrollBy(iLengthToScroll, 0);              

in onResume do nothing, but

Handler mHandler = new Handler();
Runnable scroll = new Runnable()
{
  @Override
  public void run()
  {
    HorizontalScrollView hsv = (HorizontalScrollView) findViewById(R.id.buttons);
    if (null != hsv) hsv.scrollBy(iLengthToScroll, 0);              
  }
};
mHandler.post(scroll);

do the scrolling? Handler.post adds the runnable to the UI thread, but onResume is already in the UI thread, isn't it?

Ben Williams
  • 6,027
  • 2
  • 30
  • 54
  • 1
    I suspect it's because the view is not laid out yet, so the scrolling gets clamped to 0. You could try putting `hsv.getWidth()` before `scrollBy()` to see if it's true. – bigstones May 25 '11 at 13:44
  • That does seem to be the problem; does layouting not finish until onResume has completed as well as onCreate? – Ben Williams May 26 '11 at 08:33
  • I don't remember the order these methods get executed, but probably the layout doesn't even begin until then! If you need a cleaner way to execute code immediately after layout there's [this solution](http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-re/4406090#4406090). – bigstones May 26 '11 at 10:04
  • Here is the answer how to do scrolling in your case http://stackoverflow.com/questions/16635834/android-scrollto-does-not-work-in-onresume-method?answertab=votes#tab-top – sberezin Aug 13 '14 at 16:21

0 Answers0