Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.