How to scroll ScrollView to a certain position in the text

I have a ScrollView with a TextView inside it, and I would like to scroll it to a specific paragraph, just like bindings in HTML (like page.html # paragraph_id).

Does anyone know how to do this?

Thanks!

+4
source share
3 answers

Try using the Additional Information method scrollTo .

check with code

TextView tv = (TextView)findViewById(R.id.textView);//my text view
ScrollView sv = (ScrollView) findViewById(R.id.scrollView);//my scrollview
String log ="a Very long text";
tv.setText(log);

sv.post(new Runnable() {

    public void run() {
        sv.scrollTo(0, "value till you want to scroll");
    }
}); 
+1
source

. , .
      , , - textview - lines .
       , - .
       textview by-textview.getLineheight();        scrollview
       , . .        - , .

enter image description here

, , android os ( )

mScrollView.scrollTo(0, your_text_view_with_the_line.getBottom());

+1

?

Imagine that you have a TextView or a similar element (in your case, your html # paragraph_1), then you can get the top position of this element:

TextView yourTextView = (TextView) findViewById(R.id.tv_your_text_view);
int position = yourTextView.getTop(); // you could also call yourTextView.getBottom()

After that, you can go to your position (you can put this piece of code in your onClick () method or similar to go to this position when you click on the link or button):

ScrollView sc = (ScrollView) findViewById(R.id.scroll_view);

sc.post(new Runnable() { 
    public void run() { 
        sc.scrollTo(0, position); // these are your x and y coordinates
    }
});

Note. I was expecting to yourTextViewbe in a ScrollView layout.

Hope this helps you get started.

0
source

All Articles