Text Styling With Spans in Android
Spans are the markup objects. These can be used to style the texts at the character or paragraph level. Spans are attached to text objects and give us a variety of options to the text including adding color to text, applying clickable behavior to text, scaling text size, and drawing text in a customized way. Spans can also be used to change TextPaint properties, draw on Canvas, and even change the text layout. In order to use spans, we have to use one of the classes listed below. Class Mutable Text Mutable Markup Data Structure
SpannedString No No Linear Array SpannableString No Yes Linear Array SpannableStringBuilder Yes Yes Interval Tree
All these classes extend the Spanned interface. SpannableString and SpannableStringBuilder also extend the Spannable interface. In order to use a span, we need to call setSpan(Object spans, int start, int end, int flag) on the Spannable object. Object span parameter refers to the span to apply to the text, start and end refer to the indexes of the positions of text over which spans are to be applied.
On applying span, if the inserted text is inside the span boundaries then the span automatically expend the inserted text And if the inserted text is in between start and end indices – the flag parameter will decide that span should include or exclude the inserted text.
- Spannable.SPAN_EXCLUSIVE_INCLUSIVE – Include inserted text
- Spannable.SPAN_EXCLUSIVE_EXCLUSIVE – Exclude the inserted text.
In this example, We are going to change the style of text by using the SpannableString class together with StyleSpan, UnderlineSpan, and StrikethroughSpan. A sample image is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
In the layout file, we will have a TextView. We will style this text using spans. Below is the code snippet for the activity_main.xml file.
Step 3: Working with the MainActivity.java
In the MainActivity.java file, we will first define a string that needs to be styled. Once we define the string we convert it into a Spannable String after that we define all the spans that we need for modification and then we set these spans over the spannable strings and finally set the spannable string to TextView. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Output: Run On Emulator
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.