question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextView / ScrollToEnd

See original GitHub issue

Build: 0.89.4

New to using this project so there could be a better way to do what I’m intending, I was unable to figure it out without using some of the internals. Basically, I’m using a TextView to display the contents of a running log. As new text is added from an async Task I want the text view to scroll to the end.

Flow would be:

  1. Scroll to the last line minus the frame height so that you see the last line but also the last screen of information (scrolling to just the last line with ScrollTo shows only the last line in my testing).
  2. Position the cursor on the last line.

I had attempted to add this in my code, wasn’t sure how to get the current number of lines from the TextView so I was counting line breaks which I’d prefer to avoid since it already exists. In the TextView control the model.Count has that value and it additionally has the current frame height which I couldn’t figure out how to get at from my own code. I created this function I put in TextView, I’m curious if it would be useful in some form (or if there’s a way to accomplish this in my code without adding a function to TextView).

/// <summary>
/// Will scroll the <see cref="TextView"/> to the last line and position the cursor there.
/// </summary>
public void ScrollToEnd ()
{
	// Subtract off the frame's height from the current number of lines so that
	// when we scroll to the bottom we show the last screen worth of lines and not
	// just the last line.
	topRow = Math.Max (1, model.Count - Frame.Height);
	SetNeedsDisplay ();

	// Set the cursor to the last row and then position it.
	currentRow = (model.Count > 0) ? currentRow = model.Count - 1 : 0;
	PositionCursor ();
}

I tested this in cmd and Powershell on Windows and via Putty on Ubuntu 18.04.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
blakepellcommented, Sep 2, 2020

MoveHome and MoveEnd sound good to me as well. My personal preference would be to see MoveUp and MoveDown be public as well in regards to giving the caller more options to get the TextView to do what they need it to (I won’t put that in the PR unless that’s approved). But I also understand that sometimes the original designers are trying to save us from ourselves and have intentions that are bigger picture than my use case. 😄

I tested your code and it works well. I’ll work on a PR. 😃

1reaction
BDispcommented, Sep 2, 2020

Don’t forget to add documentation to the public methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

auto-scrolling TextView in android to bring text into view
I have a TextView that I'm dynamically adding text to. in my main.xml file I have the properties set to make my max...
Read more >
TextView / ScrollToEnd · Issue #886 · gui-cs/Terminal.Gui
Basically, I'm using a TextView to display the contents of a running log. As new text is added from an async Task I...
Read more >
How to Make TextView Scrollable in Android?
1. By using ScrollView: With the help of ScrollView, you can make a view either vertically or horizontally scrollable. 2. By using the...
Read more >
android.widget.TextView.scrollTo java code examples
How to scroll to a given line number, TextView inside ScrollView. TextView textView = (TextView) findViewById(R.id.text); textView.
Read more >
How to Make Scrollable TextView in Android
Android TextView allows user to display text in android application. In this tips, I will show how to make scrollable TextView in android....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found