📜 ⬆️ ⬇️

Using RichText in Android. Spannable

Hi habrascoobschestvo! This article is about using Spannable in Android. It is intended for styling text. If you are interested in how to implement something like this:



then welcome under cat. Article focused on developers who do not have much experience developing for Android.

')

Theory


Spannable is an interface that describes the labeling of plain text with certain objects. The task of these objects is to assign part of the text of a certain style. Such marking objects can be instances of classes that implement the ParcelableSpan interface. Adding marking is carried out by the method:

Spannable.setSpan(Object span, int start, int end, int flags); 

Removal, respectively, by the method:

 Spannable.removeSpan(Object span); 

A little theory, let's go straight to the practice.

Practice


To master the practice, you must create android project or open an existing one. I created a clean project, I placed a TextView on the main activity. Go to the method where we will initialize our TextView (I have onCreate ) and add the following text:

 // Create spannable text and set style. Spannable text = new SpannableString("This is underline and bold text."); text.setSpan(new UnderlineSpan(), 8, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new StyleSpan(Typeface.BOLD), 22, 26, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // Set spannable text in TextView. TextView textView = (TextView) findViewById(R.id.text); textView.setText(text); 

So, let's see what we wrote. SpannableString is a class that implements the Spannable interface (other implementations can be found on the official documentation website ). UnderlineSpan - the implementation of ParcelableSpan, marks part of the text as underlined (in our example it is from the 8th to the 17th letter). The Spanned.SPAN_EXCLUSIVE_EXCLUSIVE flag means that our span will not expand to insert text to the left or right of the tagged part. (With all the flags can be found on the official website, when working with texts readonly they are not so important).

Also here we used another implementation of ParcelableSpan - StyleSpan . As you may have guessed, you can use it to mark text as bold ( Typeface.BOLD ) or italics ( Typeface.ITALIC ).

We figured out the code, run our project and look at the result. The text should look like this:



Well, let's go further: let's add a button to our activity, and the following text to the initializing method:

 // Create spannable text and set style. Spannable buttonText = new SpannableString("Italic text"); buttonText.setSpan(new StyleSpan(Typeface.ITALIC), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // Set spannable text in TextView. Button button = (Button) findViewById(R.id.button); button.setText(buttonText); 

At this time we will not disassemble the code, nothing new for us anymore. We used the italics marking that I described above. We start, we look.



We were able to display stylized text on the button.

Let's go even further, we will implement a button click event handler, write the following code in it:

 Spannable text = new SpannableString("Italic green text in toast"); text.setSpan(new StyleSpan(Typeface.ITALIC), 0, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.setSpan(new ForegroundColorSpan(Color.GREEN), 0, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Toast.makeText(this, text, Toast.LENGTH_LONG).show(); 

Here we have a new class - ForegroundColorSpan , which sets the color for our text, in the example we set green. We use it together with StyleSpan . Run the application, look at the result.



We were able to stuff styled text even into Toast . We also showed that for one part of the text you can use multiple style markings.

Instead of conclusion


In the example we used only some of the available styles, a larger list can be viewed on the developers site . I think you can experiment with them.

The purpose of the article was to show in an accessible form such a strong and simple tool for styling its application to developers who did not know about its existence or simply did not use it. I hope that the article will be useful.

Sample sources on Github .

Source: https://habr.com/ru/post/167343/


All Articles