Android ScrollView scrollTo () 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 has long content. Now I can scroll the ScrollView manually to the end of the contents of the TextView. In the code I write: scrview.scrollTo(0, 800); , with scview is my ScrollView. It should scroll to the middle of the contents of the TextView. But at startup, it scrolls to the beginning of the TextView and cannot scroll the contents of the TextView.

Does anyone know what causes these problems?

EDIT: I found it. Looks like I'm calling scrollTo too soon. Using

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

and it works. A posted response for anyone receiving the same issue.

+7
source share
2 answers

First of all, if all your LinearLayouts have the same orientation, why don't you just create it only once? If I am not mistaken, ScrollView can have only 1 child (1 direct child). Perhaps this is probably the cause of your problems. However, if you still want to use these 3 LinearLayouts, you can create a FrameLayout as the parent to store them and make this FrameLayout a child of the ScrollView

+2
source

I found him. Looks like I'm calling scrollTo too soon. Using

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

and it works. A posted response for anyone receiving the same issue.

+19
source

All Articles