7

Is there a way to check if a TextView's text exceeds the max lines? I've found TextView.getLineCount() but this can only tell me if I am using the max lines, not if I have exceeded it.

EDIT:
So far, I have:

text_view.post(new Runnable() {
            @Override
            public void run() {
                if (text_view.getLineCount() < text_view.getMaxLines()) {
                    // do stuff
                }
            }
});

But this does not detect if it exceeds max lines, i.e. the if statement will pass if it uses greater than or equal to max lines. This is because if it uses exceeds max lines then getLineCount just returns exactly max lines, never more.

Note that the text view's text is set programmatically higher up in my code, not hardcoded in the XML.

Paradox
  • 4,602
  • 12
  • 44
  • 88
  • What do you mean with exceeds the max lines? You want to give the max lines number? – Davide Apr 28 '18 at 01:31
  • @Davide If it exceeds the TextView's max lines which is set in the XML. To access in Java, it's TextView.getMaxLines(). So for example, if the text would need 10 lines but the max lines is 5 then lines 6-10 will be truncated and I would like to detect when this happens. – Paradox Apr 28 '18 at 01:32
  • Not sure if this is what you're looking for, but you could make use of the algorithm proposed in [this answer](https://stackoverflow.com/a/32096884/3290339) to figure out how many fully visible lines a `Textview` has. – Onik Apr 28 '18 at 17:58
  • @Onik Can you please make it as an answer? I've found a way (and wrote it as an answer here), but it's a bit like a workaround... – android developer Nov 05 '18 at 09:13
  • @android developer, in fact I don't know what criteria can be used for the solution regarding the OP's requirement. Have you tried the solution? From the top of my head, Paradox might set `android:minLines=5` and check if page count exceeds 2. However, I think there is a better solution and don't want to post my assumption until tested. – Onik Nov 05 '18 at 13:03
  • @Onik I didn't understand the solution that was proposed there. That's why I was hoping you could put your solution to check it out... – android developer Nov 05 '18 at 18:04
  • @android developer, I will if I have time. Sorry, don't have much now( – Onik Nov 05 '18 at 18:10
  • @Onik OK thank you – android developer Nov 05 '18 at 18:17

2 Answers2

2

You can do something like this:

textView.doOnLayout {
    if (textView.isEllipsized()) {
        // Do Something
    }
}

fun TextView.isEllipsized(): Boolean {
    val textPixelLength = paint.measureText(text.toString())
    val numberOfLines = ceil((textPixelLength / measuredWidth).toDouble())
    return lineHeight * numberOfLines > measuredHeight
}
1

Not sure if there is a better way, but I got it by checking what will happen if I set the maxLines to be higher, and see if after measuring it will still fit in the original maxLines.

If you want to also animate nicely from collapsed to expanded mode of the TextView, check out my solution here.

If not, here's my solution for this very specific question :

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val expectedWidthOfTextView = resources.displayMetrics.widthPixels
        val originalMaxLines = textView.maxLines
        if (originalMaxLines < 0 || originalMaxLines == Integer.MAX_VALUE)
            Log.d("AppLog", "already unbounded textView maxLines")
        else {
            textView.maxLines = originalMaxLines + 1
            textView.measure(
                View.MeasureSpec.makeMeasureSpec(expectedWidthOfTextView, View.MeasureSpec.AT_MOST),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            )
            Log.d("AppLog", "lines:${textView.lineCount}/$originalMaxLines")
            if (textView.lineCount <= originalMaxLines)
                Log.d("AppLog", "fit in original maxLines")
            else
                Log.d("AppLog", "exceeded original maxLines")
            textView.maxLines = originalMaxLines
        }

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent" android:orientation="vertical" android:gravity="center"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/textView" android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp" android:background="#1100ff00"
            android:ellipsize="end" android:maxLines="4"
            android:paddingEnd="16dp" android:paddingStart="16dp"
            android:textColor="#c1000000" android:textSize="14sp"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>

</LinearLayout>
android developer
  • 114,585
  • 152
  • 739
  • 1,270