This is really a really difficult problem in Android. If you need to know at the time you set the text (i.e. before visualizing the view or next to the visualization), you can only use StaticLayout. If you can wait until rendering, you can use viewTreeObserver:
yourTextView.getViewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout(){
yourTextView.getLineCount();
}
}
onGlobalLayout can be called several times, and sometimes (for some reason) I found that the views are not laid out yet, so you have to make some notes and see if you can find a way to make sure that any result you get is actually meaningful ( i.e. the number of rows> 0).
StaticLayouts - , . , , :
yourTextView.setText(newText);
StaticLayout measure = new StaticLayout(yourTextView.getText(), yourTextView.getPaint(),
maxAvailableWidthForYourTextView, Layout.Alignment.ALIGN_NORMAL, 1.0f, 1.0f, false)
int numberOfLines = measure.getLineCount();
( ) TextView, onSizeChanged onMeasure . , , , , , , , , , .