Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll ScrollView to certain position in the text [duplicate]

I have a ScrollView with a TextView inside it, and I'd like to scroll it to a certain paragraph, just like anchors in HTML (e.g page.html#paragraph_id).

Does anybody know a way to do it?

Thanks!

like image 788
Erez.info Avatar asked Nov 02 '22 12:11

Erez.info


2 Answers

Try using scrollTo method More Info

check with follow 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");
    }
}); 
like image 189
SilentKiller Avatar answered Nov 15 '22 03:11

SilentKiller


Really a very intresting question. I am also tyring to do so.
What I have done is an idea to map textview - lines to scrollview coordinates.
what you can do is get the device-display size - in pixels.
and a line height of the textview by- textview.getLineheight(); and calculate the line coordinates and map it to the scrollview
As I said I am also trying to do this. I am currently working on the same. If I find some code working I will let you know.

enter image description here

Its lot of work to do because android os does not provide such kind of scroll you (or I) want
OR you can have textview for each line and use this code
mScrollView.scrollTo(0, your_text_view_with_the_line.getBottom());

like image 45
johntheripp3r Avatar answered Nov 15 '22 03:11

johntheripp3r