开发者

Maximum line length on Android TextView

I'm putting a formatted single line text (no \n's) to a noneditable TextView. In the navigation of the program, the text can be changed. On some text, the TextView shrinks to 0x0 pixel and I can see nothing! I added some menus to truncate the text 10 characters each time and I found that if the number of characters are larger than 4470, the TextView shrinks.

Splitting the text into several lines by putting \n in between solves the problem, but that's not my intention.

Could you help me find if this is documented, or is there anything that ca开发者_如何学Pythonn be set to remove this limitation?


I did some testing. It seems this value is not fixed. It moved up and down each time. But there are some limits:

  • TextView.maxLines is Integer.MAX_VALUE by default (2147483647)
  • TextView.maxEms is Integer.MAX_VALUE by default
  • Java Strings in general are Integer.MAX_VALUE or half the maximum heap size

On my first test it worked up to 179 899 characters in a single line. By adding android:maxLength="10000000" to the xml I got up to 184 200. But that number worked just once. With a \n line break after every character I got up to about 750 000.

It's interesting to note that the textView never shrunk like it did back then. The app crashed instead:

Throwing OutOfMemoryError "Failed to allocate a 32 byte allocation with 0 free bytes and 0B until OOM, target footprint 536870912, growth limit 536870912" (VmSize 2143664 kB, recursive case)

I used a Pixel 3 API 29 emulator with 1536 mb ram, 256mb heap and the following code:

StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i<750000; i++) {
  stringBuilder.append("A\n");
}
textView.setText(stringBuilder);

I didn't find anything in the documentation.

The bottom line is there is no practical maximum and phones have a lot more memory today...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜