Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the number of lines visible in TextView?

Tags:

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);     
like image 886
umni4ek Avatar asked Jan 31 '14 13:01

umni4ek


People also ask

How do I limit the number of lines in a TextView?

gpetuhov/textview_limit_text. txt. This will limit number of lines to 1 and if text exceeds limit, TextView will display ... in the end of line.

What is line height in TextView?

Line height usually means text size + "padding" top/bottom. So, if your designer write line height 19sp and text size 15sp, it means you need to have extra padding 4sp. 19sp - 15sp = 4sp. To implement it in your layout, use lineSpacingExtra attribute.

How do you fit text in TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.


1 Answers

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?

like image 200
Yaroslav Mytkalyk Avatar answered Sep 25 '22 16:09

Yaroslav Mytkalyk