Cover image by Lauren Peng on Unsplash

1. Introduction

Since Android API 23 (Android 6.0), TextView has added a breakStrategy property, which controls the line breaking strategy for dividing text into multiple lines. In layman’s terms, it determines how much text to display on a line.

BreakStrategy can be set either by the TextView’s XML attribute Android :breakStrategy or by the setBreakStrategy method. There are only three values that can be set, and they are the three constants of the Android.Text.Layout class:

  • BREAK_STRATEGY_SIMPLE: corresponds to the XML attribute “simple”

  • BREAK_STRATEGY_HIGH_QUALITY: corresponds to the XML attribute “high_quality”

  • BREAK_STRATEGY_BALANCED: corresponds to the XML attribute “balanced”

2. Comparison of three line folding strategies

2.1 BREAK_STRATEGY_SIMPLE

Simple folds. This strategy will display as many characters as possible, in each row until the line does not show more characters to a line break, at the same time, under this strategy will not automatically add conjunction operator (the official document said, when a row is only one word and the width of the display more than it will add a conjunction, but in the process of testing and didn’t see the conjunction operator).

During text editing, the text added later does not affect the layout display of previous text, and is more suitable for editable text. EditText’s default line folding policy is this, because it avoids the problem of character jumping caused by layout refreshes when typing text, and ensures the user’s input experience.

2.2 BREAK_STRATEGY_BALANCED

Balance folding. This strategy ensures that every line of a paragraph is as wide as possible, adding conjunctions if necessary.

2.3 BREAK_STRATEGY_HIGH_QUALITY

High quality folding. This strategy optimizes the layout for folding entire sections of text and automatically adds conjunctions if necessary. This strategy has a slight performance impact and requires more time for text layout than the other two strategies. This policy is usually better for read-only text, as is the default line folding policy for TextView.

Text introduction is not as intuitive as pictures. Here is a simple example to show the difference of paragraph folding under different strategies.

It should be noted that setting three different strategies for the same text does not necessarily produce three different layouts, but more often, two of them have the same layout. To highlight the layout differences between these three strategies, I chose text that was mixed with numbers and ImageSpan (this was also the test case I encountered that caused the layout to be abnormal).

BREAK_STRATEGY_SIMPLE Fold effect

BREAK_STRATEGY_HIGH_QUALITY Folding effect

BREAK_STRATEGY_BALANCED

3. Source code analysis

TextView uses StaticLayout or DynamicLayout to set the corresponding line folding strategy. In the latest source code, Layout uses LineBreaker to do line folding.

LineBreaker, a new addition to API 29, provides the computeLineBreaks method for calculating paragraph breaks. The results are wrapped in an instance of the LineBreaker.Result class, which provides methods for obtaining the results of a broken line calculation. Such as getLineCount, getLinewidth, and so on.

If you are interested in the implementation details, you can read the source code further, but HERE I will only throw out a brief introduction.

4. Low version performance

Since the breakStrategy attribute was only added in API 23, it is important to understand the text line folding strategy in earlier versions. This way, when we use this property, we can make sure we know what we’re doing, and try to avoid experience problems with compatibility.

Conclusion first, based on my (not rigorous) tests, the line folding strategy used by TextView in earlier versions was Simple.

Test process: I used simulators of the same model, with one running system version 19 and the other running system version 24. Under the same display contents, I found by comparison that, The layout of the lower version is the same as that of the higher version when simple is set. (This text appears in a different layout when the folding policy is balanced and high_quality.)

API 19 Line folding effect

API 24 BREAK_STRATEGY_SIMPLE Fold effect

5. To summarize

Starting with API 23, TextView added the breakStrategy property to control the folding strategy for dividing a text into multiple lines. The three strategies are simple folding, balanced folding, and high quality folding.

Simple line folding aims to show as much text on one line as possible; Balanced line folding focuses on making the width of multiple lines of text in a paragraph as consistent as possible. High-quality line folding optimizes the text layout from paragraph to paragraph as a whole and is more costly in performance than the other two.

TextView uses high_quality as its default line folding policy, while EditText uses simple as its default line folding policy to ensure input experience and prevent character jumping during input.

From the source code, breakStrategy is implemented primarily through StaticLayout, which in the latest version of the source code uses a LineBreaker (new in API 29) internally to branch paragraphs.

Prior to API 23, the line folding strategy for text was simple in older versions.

The breakStrategy property may be less common than other properties, but if you find that TextView does not fold as well as you expected (especially when dealing with mixed Text and images, etc.), you may get the result you want by changing the value of this property.

Recommended reading

  • Summary of common Soft Keyboard Operation in Android Development (V1.0)
  • SparseArray source code parsing
  • ArrayMap source code parsing