24

I'm setting some text on a TextView every 0.5 seconds based on a timer. Everytime, when the timer runs and the text is set, I'm getting this warning message being spammed in my console.

W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1

XML Code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="12dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@color/white"/>

    <TextView
        android:id="@+id/time_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="4dp"
        android:paddingStart="12dp"
        android:textColor="@color/white"
        android:textSize="12sp"
        tools:text="0:00" />
</RelativeLayout>

Java Code:

public void setProgress() {
    // setProgress() called every 0.5 seconds
    // Hardcoded text
    mTimeText.setText("0:05");
}
mco
  • 1,809
  • 15
  • 31
  • Can you post the code that you use for displaying test? – J K Mar 21 '18 at 20:44
  • @JK Yea sorry. I just added it. – mco Mar 21 '18 at 20:50
  • One thing is unclear for me. Why do you call `DateTimeUtils.formatTimeFromMillis(timeMs));`? Is the class containing the `formatTimeFromMillis` named `DateTimeUtils`? In the code you given it it looks like these methods are in the same class. – J K Mar 21 '18 at 20:56
  • Sorry for the confusion, I removed the `DateTimeUtils` part. – mco Mar 21 '18 at 20:58
  • I can't see any error in this code... And you can just replace formatTimeFromMillis method with hardcored string, would be much easier to read. – J K Mar 21 '18 at 21:05
  • I'm not sure if it helps, but try this: https://stackoverflow.com/questions/37754299/how-to-properly-set-line-height-for-android – J K Mar 21 '18 at 21:09
  • @JK I solved the issue, it's a bizarre one. – mco Mar 21 '18 at 23:19

3 Answers3

25

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.

mco
  • 1,809
  • 15
  • 31
  • That's really weird - but thank you for the solution. I had this effect only on Nokia devices (Android One). Setting ``.setSingleLine(true)`` in code not only removed the warning, the UI got significantly more performant! I'd give +10 if I could :-) – Grisgram Sep 13 '18 at 12:53
  • Why do you say android:singleLine="true" xml is deprecated ? Adding this line to my TextView's xml description fixed the problem and it does appears as deprecated ! – matdev Feb 22 '19 at 09:58
  • `maxLines="1"` doesn't show me any deprecation warning in Android Studio. – Mukesh Sai Kumar May 19 '20 at 11:18
5

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

drc
  • 275
  • 3
  • 9
2

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"