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);
ab11
  • 19,770
  • 42
  • 120
  • 207

2 Answers2

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

Community
  • 1
  • 1
Frank
  • 12,010
  • 8
  • 61
  • 78
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 :)

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • 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 '11 at 19:46