Spantastic text styling with Spans

<p>To style text in Android, use spans! Change the color of a few characters, make them clickable, scale the size of the text or even draw custom bullet points with spans. Spans can change the&nbsp;<code>TextPaint</code>&nbsp;properties, draw on a&nbsp;<code>Canvas</code>, or even change text layout and affect elements like the line height. Spans are markup objects that can be attached to and detached from text; they can be applied to whole paragraphs or to parts of the text.</p> <p>Let&rsquo;s see how to use spans, what spans are provided out of the box, how to easily create your own and finally how to test them:</p> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#b0e9" rel="noopener ugc nofollow">Styling text in Android</a></p> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#d5d9" rel="noopener ugc nofollow">Applying spans</a></p> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#149b" rel="noopener ugc nofollow">Framework spans</a></p> <ul> <li><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#f6db" rel="noopener ugc nofollow">Appearance vs metric affecting spans</a></li> <li><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#1861" rel="noopener ugc nofollow">Character vs paragraph affecting spans</a></li> </ul> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#7657" rel="noopener ugc nofollow">Creating custom spans</a></p> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#e345" rel="noopener ugc nofollow">Testing custom spans implementation</a></p> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568#eef0" rel="noopener ugc nofollow">Testing spans usage</a></p> <h1>Styling text in Android</h1> <p>Android offers several ways of styling text:</p> <ul> <li><strong>Single style</strong>&nbsp;&mdash; where the style applies to the entire text displayed by a TextView</li> <li><strong>Multi style</strong>&nbsp;&mdash; where several styles can be applied to a text, at character or paragraph level</li> </ul> <p><strong>Single style</strong>&nbsp;implies styling of the entire content of the TextView, using XML attributes or&nbsp;<a href="https://developer.android.com/guide/topics/ui/look-and-feel/themes.html" rel="noopener ugc nofollow" target="_blank">styles and themes</a>. This approach is an easy solution and works from XML but doesn&rsquo;t allow styling of parts of the text. For example, by setting&nbsp;<code>textStyle=&rdquo;bold&rdquo;</code>, the entire text will be bold; you can&rsquo;t define only specific characters to be bold.</p> <p><a href="https://medium.com/androiddevelopers/spantastic-text-styling-with-spans-17b0c16b4568">Read More</a></p>