Android TextView class is a subclass of View class and is designed to hold and display text. It can be used in the code by importing the package
android.widget.TextView. Typically, a TextView isn’t meant for editing. For editing we use
Text view is most important widgets in while developing android applications.Simply you can not develop any applications without using this.
In this series We will discuss following things.
- TextView Architecture
- Styling text.
- Text Layout in View
In first article we will discuss about TextView Architecture and Styling Text.
Text stack in android is split in two parts, there is java code and there is native or C++ code.
1. Java layer-At the top of java layer(as you can see in below diagram) two widgets TextView and EditText is available.For custom view implementation below this layer second layer exists which consist Layout,Paint and Canvas.This layer helps to layout text and render it without using widgets.
2.Native layer- At the top of native layer Minikin library exists.Minikin helps to do word measurements, line breaking and hyphenation.Below this ICU library exists which deals with unicode, HarfBuzz does text shaping, FreeType for conversion of Gifs to bitmaps, Skia which is graphics engine.
Minikin-It lies at the core of android stack and is implemented in c++.It’s main responsibilities are text layout measurement and line breaking.
i).Text Measurement-Minikin took string as parameter and identify the glyphs(Glyph is a representation of character similar to image representation of character).
Glyph matching is not necessarily one to one ,it can also be found in different fonts.Once all the glyphs are identified system can position them to have final look.When we provide longer strings minikin first break it into words and for each word it does the measurements the result of measurements is added into a LRU cache called Word Layout cache(It has fixed size of 5k items) so that if system come across same word again so cached value can be used instead of re measurements.
ii).Line Breaking- When the string is wider than the array in which text to fit in then minikin has to do a line break.
In simple case boxes are put side by side until boundary is reached an then moving to next line.this behaviour can be controlled using break strategy.
In balanced strategy minikin will distribute the words among the lines to have better text alignment. Default value is high quality which is vey similar to balanced except some subtle differences like hyphenation for last line.
iii)Hyphenation-Hyphenation improves the alignment of text and use of whitespace.It arises the measurements costs because now there will be more words to be measured an it has to compare more configuration for optimum solution.
Performance in Android P
Hyphenations is also affected by locale.We have to explicitly set locale for other language string.If string have multiple language then we have to set the multiple locale span which downgrade text layout performance.
When we want to set multiple style in a text we have to use span.
Spans are markup objects that can be attached to text.Spans can be characterised into character and paragraph spans depending on whether they apply to only a few characters or to entire paragraph.
- Character spans can be split into appearance affecting and metric affecting.
Appearance affecting(i.e. background color span) requires redraw method of textview to be called while metric affecting(i.e. TypeFace span for font change) requires both remeasure and redraw method to be called because in this case size of text is also changing.
- Paragraph spans are used to style blocks of text like changing the layout margin or drawing bullets. Bullets span is used to do these things.
Note:- Comman use case spans are already available in framework and you can still create new Custom span. But you should use Framework span because only these spans can be parceled.
When we require parcelling of span-
- When we pass text via intents.
- When we copy text text is parcelled and then unparcelled via clipboard service.
How to use span- To use spans two key interface are available spanned and spannable.
Spanned is for immutable markup and immutable text,have declaration of methods like getSpan(),getSpanStart etc.It will allow only query span not modify them.
Spannable is for immutable text and mutable markup. It allow to set and remove span.
Three concrete class implementation of these two interfaces is available as shown in above diagram.
SpannableString hold array of spans whereas SpannableStringBuilder hold tree of spans.
Note:- Upto 250 spans performance of SpannableString and Spannable StringBuilder is same but after that SpannbleStringBuilder is faster in rendering.
Note:For checking if one span is present in spannable use Spanned.nextSpanTransition.
Styling internationalised text-When we use multiple language ,we have multiple string files.In different language same text can be appear in different places so indexing is different for spannable. HTML can we used in string xml but they have limited functionality.
We should use annotation tags for achieving this.Annotation tags allows to set key and value pairs.
To be continued……….
Stay tuned for further articles on this series.
Check post about Battery optimisation in android.