How to determine the number of lines visible in TextView?

android, lines, textview

Solution

android.text.Layout contains this information and more. Use `textView.getLayout().getLineCount()` to obtain line count.

Be wary that `getLayout()` might return `null` if called before the layout process finishes. Call `getLayout()` after `onGlobalLayout()` or `onPreDraw()` of `ViewTreeObserver`. E.g.

textView.getViewTreeObserver().addOnPreDrawListener(() -> {
    final int lineCount = textView.getLayout().getLineCount();
});

If you want only visible line count you should probably use the approach mentioned in the answer below:

Is there a way of retrieving a TextView's visible line count or range?

Problem

how to get how many lines are displayed in visible part of `TextView`? I use text, which not fully placed in `TextView` on every screen resolution. ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/logs_text" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> String s = "very big text" TextView logText = (TextView) view.findViewById(R.id.logs_text); logText.setText(s); ```

Original source

Related problems