How can I get the number of lines a string will take up in a TextView
before it is rendered.
A ViewTreeObserver
will not work because thos
If you know or can determine the width of the TextView's parent, you are able to invoke a view measurement which results in line count being calculated.
val parentWidth = PARENT_WIDTH // assumes this is known/can be found
myTextView.measure(
MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
The TextView's layout
is no longer null and you can check the calculated line count with myTextView.lineCount
.