Search code examples
androidtextview

TextView's inconsistent paragraph breaks on long words


I have a textview which contains a static text that is long. When viewed on different Android versions (Lollipop and Nougat), some words are broken up differently even though devices have the same screen size and density.

Here is a sample output on Nougat:

| This is my applic-|
| ation that shows  |
| long paragraphs.  |

However, when viewed on Lollipop, here is its output:

|    This is my     |
|  application that |
|    shows long     |
|    paragraphs.    |

Solution

  • Break strategy for paragraphs were added in Marshmallow, thus the difference in behavior.

    Sets the break strategy for breaking paragraphs into lines. The default value for TextView is Layout#BREAK_STRATEGY_HIGH_QUALITY, and the default value for EditText is Layout#BREAK_STRATEGY_SIMPLE, the latter to avoid the text "dancing" when being edited.

    Pre-Marshmallow, textviews behave as if they are set with BREAK_STRATEGY_SIMPLE break strategy. Thus, to get a uniform behavior, use the following:

    <TextView
        ...
        android:breakStrategy="simple" />