Android TextView: Getting "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" when setting text

Answering my own question.

Notice how I have two TextView's title_text and time_text. Commenting out //mTimeText.setText("0:05"); solved the issue of the warning message being spammed, so I thought the issue had to do something with time_text, but it didn't.

It had to do with title_text. Notice how I set the properties android:maxLines="1" and android:ellipsize="end". If I had text that would overflow past the maxLines limit and trigger the ellipses, then I would get the warning message. Removing the line android:ellipsize="end" solved the issue. However, I need the ellipses, so that's not going to work.

The only other solution I could come up with is replacing android:maxLines="1" with android:singleLine="true", however that xml property is deprecated!

Therefore, I just set mTitleText.setSingleLine(true) programmatically in my java code. That method isn't deprecated so I think I'm in the clear.

As to why commenting out //mTimeText.setText("0:05"); prevented the warning message from showing up, I don't really know. I'm stumped on that one.


This is a bug in Android which is marked as fixed, so we'll have to wait for the next patch: https://issuetracker.google.com/issues/121092510


I got it solved by changing the TextView's layout_height configuration from wrap_content to match_parent. Defining a fixed layout_height is another way to solve it.


Examples:

android:layout_height="wrap_content" or android:layout_height="20dp"