8

How to get the maximum number of lines that can be written in TextView? I tried this:

DisplayMetrics metrics = new DisplayMetrics ();
getWindowManager (). getDefaultDisplay (). getMetrics (metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;

int lines = (int) (height / textView.getLineHeight ());

System.out.println (lines);

However, this variant gives the wrong information.

Kyborg2011
  • 608
  • 3
  • 11
  • 29

3 Answers3

4

textview.getLineCount()

It's all in the documentation

EDIT: I was a bit too fast. I assume you want the number of lines that CAN be inside a textview instead of how many there currently ARE in the textview... In that case -> have a look at this question: Calculate text size according to width of text area

Community
  • 1
  • 1
Entreco
  • 12,738
  • 8
  • 75
  • 95
1

textview.getLineCount() will help you

0
textView.getHeight() / textView.getLineHeight()

textView.getHeight() and textView.getLineHeight() both will consider the height on the screen with the screen dpi.

Here is the post to get the correct height of view. View's getWidth() and getHeight() returns 0

Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36
Yuda
  • 1
  • 2