23

I wonder if there are any ways to detect either if string to be fitted into a text view reaches the end of screen and therefore change line, or if a textview spans over 2 lines?

I want to know this so I can increase margins between some textviews if a textview spans over 2 lines, of course I want to do this dynamically.

Robert
  • 4,602
  • 4
  • 22
  • 33

1 Answers1

39

TextView supports getLineCount() - See docs careful as you may need to check if the view has been drawn first.

Chris.Jenkins
  • 13,051
  • 4
  • 60
  • 61
  • Thanks! It works like a charm, as long as it's being used after the layout has been drawn. – Robert Nov 19 '12 at 16:50
  • 2
    As a side note since I discarded this answer at first: you may need to call `textView.onPreDraw()` if you just changed the view contents; otherwise the value returned by `getLineCount()` will be 0 – Romuald Brunet Sep 13 '18 at 21:15
  • if `result 0` then try this https://stackoverflow.com/a/32340660/9868485 – code4rox Mar 30 '21 at 11:57