Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ScrollView scrollTo() method not working with TextView?

For Example, I have a layout like this:

<ScrollView>
  <LinearLayout>
    <LinearLayout>
         ...
    </LinearLayout>
    <LinearLayout>
         ...
    </LinearLayout>
    <LinearLayout>
         <TextView>
         </TextView>
    </LinearLayout>
  </LinearLayout>
</ScrollView> 

My TextView have long content. Now I can scroll the ScrollView manually to the end of TextView content. In the code, I write: scrview.scrollTo(0, 800);, with scrview is my ScrollView. It supposed to scroll to the middle of the TextView content. But when running, it only scrolled to the start of the TextView and can't scroll through the TextView content.

Does anybody know what causing this problems?

EDIT: I found it. Seems like I call scrollTo too soon. Using

scrview.post(new Runnable() {
                public void run() {
                    scrview.scrollTo(0, 800);
                }
            }); 

instead and it works. Posted the answer for anyone getting same problem.

like image 591
user1417127 Avatar asked Jun 28 '12 07:06

user1417127


2 Answers

I found it. Seems like I call scrollTo too soon. Using

scrview.post(new Runnable() {
                public void run() {
                    scrview.scrollTo(0, 800);
                }
            }); 

instead and it works. Posted the answer for anyone getting same problem.

like image 71
user1417127 Avatar answered Oct 10 '22 15:10

user1417127


First of all, if all your LinearLayouts have the same orientation, why you don't simply create it only once? If I'm not wrong, ScrollView can only have 1 child (1 direct child). Maybe that's probably the reason of your problems. However, if you still want those 3 LinearLayouts, you can create a FrameLayout as a parent to hold them and make that FrameLayout the child of the ScrollView

like image 39
gian1200 Avatar answered Oct 10 '22 17:10

gian1200