Android Java: SpannableStringBuilder

SpannableStringBuilder() is a class for styling a specific length of a string.
The syntax in Java:

SpannableStringBuilder() span = new SpannableStringBuilder(string); 
 span.append(); <- you can put the string value in append() or just do 
 like the above line

 span.setSpan(SpanStyleClass, startIndexOftheStrYouWantToApplyEffects, endIndex, FLAG); 
 //The flag is usually calling from Spanned.something or 
 Spannable.something, they have no big difference

When setting span to a string (usually TextView), you can use styling classes:

span.setSpan(RelativeSizeSpan(text size in float),start,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 span.setSpan
 (AbsoluteSizeSpan(text size in int, boolean isTextSizeInDp),start,end,flag);
 span.setSpan(StrikeThroughSpan(),start,end,flag); 
 //set a strike through line to a string (usually TextViews)

You can also use StyleSpan() class:

span.setSpan(new 
 StyleSpan(Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 span.setSpan
 (new StyleSpan(Typeface.ITALIC), 9,15,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Note: SpannableStringBuilder is a: CharSequence, Spannable, Editable, etc…so you can return these dataTypes when using SpannableStringBuilder
Reference:
Casting between SpannableStringBuilder and CharSequence from StackOverflow

PS: Due to work reasons, I was kind of forced to use Kotlin in the past two months to build Android applications before I even started learning the language. This OOP language is designed specially for Android applications development. With its characteristics of functional programming, Kotlin has became a simpler and may be easier android programming language (if you get used to it in time).
Well, the thing is that the Kotlin category in the top menu is still empty, so I think I may try to fill it with few posts sometime…

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.