Scrolling TextView to text position

I want to scroll my TextView to make a specific position in the text visible. How can i do this? I tried showPointIntoView (int offset) but without success.

Source:

public class TextScrollActivity extends Activity { public void onCreate (final Bundle savedInstanceState) { super.onCreate (savedInstanceState); final int position = 500; final TextView textView = new TextView (this); final ScrollView scrollView = new ScrollView (this); scrollView.addView (textView); Button button = new Button (this); button.setText ("Scroll to " + position); LinearLayout layout = new LinearLayout (this); layout.setOrientation (LinearLayout.VERTICAL); layout.addView (scrollView, new LayoutParams (LayoutParams.FILL_PARENT, 200)); layout.addView (button, new LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); StringBuilder builder = new StringBuilder (); for (int i = 0; i < 1000; i++) builder.append (String.format ("[ %05d ] ", i)); textView.setText (builder); setContentView (layout); button.setOnClickListener (new OnClickListener () { public void onClick (View v) { System.out.println (textView.bringPointIntoView (position * 10)); // scrollView.scrollTo (0, position * 10); // no } }); } } 
+2
source share
3 answers

For those who have the same problem, I finally made my own implementation of showPointIntoView:

  public static void bringPointIntoView (TextView textView, ScrollView scrollView, int offset) { int line = textView.getLayout ().getLineForOffset (offset); int y = (int) ((line + 0.5) * textView.getLineHeight ()); scrollView.smoothScrollTo (0, y - scrollView.getHeight () / 2); } 

Feel free to have a better solution.

+3
source

Does adding a move method to the textual representation of the problem help?

 textView.setMovementMethod(new ScrollingMovementMethod()); 
+1
source

Just FYI for anyone else with the same problem, on a listView with a large listItems , an overloaded bringPointIntoView can be passed a listView instead of a ScrollView and use the ScrollTo smoothScrollTo method smoothScrollTo .

+1
source

Source: https://habr.com/ru/post/1344653/


All Articles