1

I have a ScrollView which I'm adding a number of custom Views to. After adding the custom Views, I would like to be able to Scroll to a particular one. But, immediately after adding the custom views, the ScrollView hasn't been resized, so I can not scroll. Any idea how I can force this resize?

for(int i = 0, i < numberOfViews, i++)
{
  scrollView.addView(new MyView(...));
}

//tried this, doesn't seem to be helpful 
scrollView.requestLayout();

//at this point scrollView dimensions are 0,0,0,0 
scrollView.scrollTo(5, 200);
0

2 Answers 2

0

I'm not completely sure what you're trying to do, but I wanted to scroll all the way down, and couldn't just do that: I had to do it like this:

((ScrollView) findViewById(R.id.yourId)).post(new Runnable() {
         public void run() {
             ((ScrollView) findViewById(R.id.yourId)).fullScroll(View.FOCUS_DOWN);
         }
 });

It will update later, so your view is made :)

1
  • Thanks, I tried this. Even if I use the post(...) method, the ScrollView height is still 0 when the fullScroll(...) method executes (I can tell by debugging, and placing a breakpoint in the fullScroll(...) method).
    – ab11
    Jan 12, 2011 at 19:46
0

An elegant solution is to plan the scroll and do it on the next onLayout(). Example code here:

https://stackoverflow.com/a/10209457/1310343

Your Answer

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

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