Mobile Development 12 min read

How We Fixed Android Chat Text Freeze: Custom TextView Layout and Performance Insights

This article recounts the discovery, root‑cause analysis, and engineering solution for a severe Android WeChat chat freeze bug, detailing a custom TextView layout that improves text alignment, handles edge‑case languages, and delivers near‑native performance.

WeChat Client Technology Team
WeChat Client Technology Team
WeChat Client Technology Team
How We Fixed Android Chat Text Freeze: Custom TextView Layout and Performance Insights

Background

Just before the National Day holiday, many Android WeChat users reported that receiving or sending messages like "15......" caused the chat UI to freeze and the app to crash. The team quickly released an emergency fix that covered most users within two days.

Root Cause

Analysis by community experts showed the freeze was caused by an infinite while loop. The loop’s condition relied on dVar2 becoming null, but a variable wwk always stayed at 0, so the condition never changed. The loop is part of the text‑segmentation algorithm that formats the message line by line.

Why the Need

Users complained that Android chat bubbles looked less polished than iOS, mainly because extra padding forced line breaks at awkward positions. A comparison of iOS and Android bubbles showed Android’s default TextView left noticeable gaps, especially when the remaining space couldn’t fit another character.

Design Requirements

To improve readability, the team defined four layout rules:

Allow at most one character‑width of spacing adjustment per line.

Avoid placing punctuation at the beginning of a line.

Never split English words or numbers across lines.

For full‑width symbols (e.g., 「」【】) that cause extra padding, reduce the padding when they appear at line edges.

Implementation

Rather than modifying the system TextView, a custom view was built. Android’s text rendering is delegated to three layout classes— StaticLayout, DynamicLayout, and BoringLayout —with StaticLayout being the most common. Because these classes lack public APIs, the custom view uses reflection to adjust spacing according to the rules.

Key reasons for choosing a custom view:

Compatibility across Android 2.3 to 8.0 would be hard to maintain with reflection.

The TextView component is complex; re‑implementing all layout logic would be labor‑intensive.

A custom layout provides more flexibility without significantly impacting performance.

Demo Results

The following screenshots illustrate the before‑and‑after effects of applying the four rules, showing noticeably tighter and more balanced line breaks.

Additional Challenges

Small‑language support (e.g., Thai, Arabic) required a quick regex check to bypass the custom layout for strings that need special handling. The regex adds less than 1 ms overhead, making it negligible.

Performance Evaluation

Frame‑rate tests on high‑end and low‑end devices showed only a minor difference: the custom view achieved 53.9 FPS versus 54.1 FPS for the system view on a good device, and 41.9 FPS versus 41.4 FPS on a weaker device. Timing measurements indicated the custom view’s setText and drawing operations were roughly twice as costly, but the overall impact on user‑perceived performance was minimal.

Conclusion

The custom layout successfully resolves the chat freeze bug, improves visual consistency across platforms, and maintains acceptable performance. The experience highlights the importance of thorough root‑cause analysis, careful design of UI rules, and rigorous performance testing when extending core Android components.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PerformanceAndroidmobile UITextViewBug FixCustom Layout
WeChat Client Technology Team
Written by

WeChat Client Technology Team

Official account of the WeChat mobile client development team, sharing development experience, cutting‑edge tech, and little‑known stories across Android, iOS, macOS, Windows Phone, and Windows.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.