Ask Your Question
4

What is the method to determine the height of a particular line in a textview?

asked 2023-07-11 06:53:13 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-11 06:59:02 +0000

woof gravatar image

One possible method to determine the height of a particular line in a textview in Android is:

  1. Get the layout of the textview by calling getTextLayout() method.
  2. Use the getPositionForOffset() method to get the start position of the line whose height you want to determine.
  3. Use the getLineTop() and getLineBottom() methods to get the top and bottom position of the line.
  4. Compute the height of the line by subtracting the top position from the bottom position.

Here's an example code snippet:

TextView textView = findViewById(R.id.my_text_view);

// Get the layout of the textview
Layout layout = textView.getLayout();

// Get the start position of the second line
int lineStart = layout.getPositionForOffset(textView.getLayout().getLineEnd(1));

// Get the top and bottom position of the second line
int lineTop = layout.getLineTop(1);
int lineBottom = layout.getLineBottom(1);

// Compute the height of the second line
int lineHeight = lineBottom - lineTop;

In this example, we are getting the layout of a textview, then getting the start position of the second line using the getPositionForOffset() method. We then get the top and bottom position of the second line using getLineTop() and getLineBottom() methods, and finally compute the height of the second line by subtracting the top position from the bottom position. The result is the height of the second line in pixels.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-11 06:53:13 +0000

Seen: 12 times

Last updated: Jul 11 '23