Usability of BoringLayout

android, android-custom-view

Solution

`BoringLayout` is used to draw text on a view. It is called "boring" because it only handles a single line of left-to-right text without any interesting characters such as emoji. This simplification allows the class to override `onDraw` with more efficient logic than the default does. Here is the source code if you want to see for yourself.

Like `StaticLayout` and `DynamicLayout`, the `BoringLayout` is also a subclass of the abstract `Layout` class. As the documentation says, you probably wouldn't use these classes directly unless you are making your own text handling widget. How do you know if you should be using one of these classes? If you are thinking about using `Canvas.drawText` in your custom view, then you should probably think about using a `Layout`. They also eventually call `Canvas.drawText`, but they do a lot of other processing beforehand.

If you are making your own text widget, then you would only use the `BoringLayout` for single line, simple, left-to-right text. For multi-line and more complex text use a `StaticLayout`. And if you need to dynamically change the text after creation, then use a `DynamicLayout`.

Problem

I'm writing a custom layout that will manage text. Before I started implementing the `ViewGroup#onMeasure()` method I started to dig the EditText source code, specifically at the `EditText#onMeasure()` method. So I came across the `BoringLayout`. I read the docs but I didn't find much explanation on it and how to use it in an actual custom implementation. Then my question is how can I use it the right way and when it is really needed.

Original source

Related problems