5

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!

Erez.info
  • 125
  • 1
  • 2
  • 11
  • I am not smart at HTML, If you want to scroll the scrollview programmatically, Can achieve by following ScrollView sv = (ScrollView)findViewById(R.id.scrl); sv.scrollTo(0, sv.getBottom()); or sv.scrollTo(5, 10); – Rethinavel Oct 25 '13 at 07:00

3 Answers3

1

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");
    }
}); 
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
1

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());

johntheripp3r
  • 989
  • 6
  • 15
  • Why bother so much? Just split it into different TextViews and use the above solution... – Erez.info Oct 25 '13 at 08:00
  • 1
    @Erez.info Making so much objects will consume more heap. So... And I have specified the you can use the one textview per line. but suppose you have 500 lines. then this is serius issue – johntheripp3r Oct 25 '13 at 08:04
  • You're right, but in my case there is only 10 paragraphs, so this soultion is excellent. Anyway, I'd like to see your code when you're finished – Erez.info Oct 25 '13 at 08:13
0

Did you try this?

Imagine you have a TextView or similar element (in your case your html#paragraph_1), then you could get the Top Position of this element by:

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

After that you could scroll to your position (you could put this code snippet in your onClick() method or similar, to jump to this position when clicking on a 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 expected yourTextView to be in the ScrollView layout.

Hope this helps you getting started.

Community
  • 1
  • 1
Michael
  • 3,982
  • 4
  • 30
  • 46