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!
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!
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");
}
});
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.
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());
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.