Android: Set rich display effects for TextView with spannablestring

Source: Internet
Author: User

In the process of using textview, it is sometimes necessary to do a series of text in some of the text to do special display effect processing, such as bold, change color, highlighting, hyperlinks and so on, we can through a number of textview patchwork to achieve, but if the need for special processing of content in the middle of the entire text, It is too cumbersome and too low to cobble together multiple TextView, and the Android-provided Spannablestring class is a great way to solve this problem, and spannablestring combines the following classes that end with span to make a rich and varied display.


This article combined with the usual work situation, wrote a Spannableutils tool class, convenient for future use, Spannableutils.java specific code as follows, if you want to set the same string of text at the same time a variety of display effects, you can consider the transformation of the class implementation:

/** * Spannable Tool class for setting text foreground color, background color, Typeface, bold, italic, font size, hyperlink, strikethrough, underline, subscript, etc. * * */public class Spannableutils {private Spannableutils () {}/** * Changes the font size of a paragraph of text in a string * Settextsize ("", 24,0,2) = null; * Settextsize (null,24,0,2) = null; * Settextsize ("abc", -2,0,2) = null; * Settextsize ("abc", 24,0,4) = null; * Settextsize ("abc", 24,-2,2) = null;  * Settextsize ("abc", 24,0,2) = Normal String * */public static spannablestring settextsize (string content, int startIndex, int endIndex, int fontSize) {if (Textutils.isempty (content) | | fontSize <= 0 | | startIndex >= ENDINDEX | | startin Dex < 0 | | EndIndex >= content.length ()) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (New Absolutesizespan ( FontSize), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring Settextsub (String content, int startIndex, int endIndex) {if (Textutils.isempty (content) || StartIndex < 0 | | EndIndex >= Content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (new Subscriptspan (), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring Settextsuper (String content, int startIndex, int endIndex) {if (Textutils.isempty (content ) || StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (new Superscriptspan (), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;}  public static spannablestring Settextstrikethrough (String content, int startIndex, int endIndex) {if (Textutils.isempty ( Content) | | StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (nEW Strikethroughspan (), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring Settextunderline (String content, int startIndex, int endIndex) {if (Textutils.isempty (cont ENT) | | StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (new Underlinespan (), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;}  public static spannablestring Settextbold (String content, int startIndex, int endIndex) {if (Textutils.isempty (content) || StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (New Stylespan ( Android.graphics.Typeface.BOLD), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring SetTextItalic (String content, int startIndex, int endIndex) {if (Textutils.isempty content) | StartIndex < 0 | | EndIndex & gt;= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (New Stylespan ( Android.graphics.Typeface.ITALIC), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring Settextbolditalic (String content, int startIndex, int endIndex) {if (Textutils.isempty (con Tent) | | StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (New Stylespan ( Android.graphics.Typeface.BOLD_ITALIC), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring Settextforeground (String content, int startIndex, int endIndex, int foregroundcolor) {if (T Extutils.isempty (Content) | | StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (New Foregroundcolorspan ( Foregroundcolor), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring Settextbackground (String content, int startIndex, int endIndex, int backgroundcolor) {if (T Extutils.isempty (content) | | StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (New Backgroundcolorspan ( BackgroundColor), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} /** * Set Text hyperlinks * @param content needs to be processed text * @param startIndex * @param endIndex the beginning and ending index of the string to be processed in the text * @param URL text corresponding to the link address, Note the format: * (1) The phone starts with "Tel:", such as "tel:02355692427" * (2) The message starts with "mailto:" such as "Mailto:[email proTected] "* (3) SMS with" SMS: ", such as" sms:02355692427 "* (4) MMS with" MMS: ", such as" mms:02355692427 "* (5) Map with" Geo: ", such as" Geo : 68.426537,68.123456 "* (6) Network with" http://", such as" http://www.google.com "* */public static spannablestring Settexturl ( string content, int startIndex, int endIndex, String URL) {if (Textutils.isempty (content) | | StartIndex < 0 | | endind Ex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content), Spannablestring.setspan (new Urlspan (URL), StartIndex, EndIndex, spanned.span_exclusive_exclusive); return spannablestring;} public static spannablestring settextimg (String content, int startIndex, int endIndex, drawable drawable) {if (textutils. IsEmpty (content) | | StartIndex < 0 | | EndIndex >= content.length () | | StartIndex >= endIndex) {return null;} spannablestring spannablestring = new spannablestring (content); Spannablestring.setspan (new Imagespan (drawable), StartIndex, EndIndex, Spanned.span_exclusive_exclusive); return spannablestring;}} 

This is a demo effect using the tool class:



This example demo can be downloaded here: spannable instance

Android: Set rich display effects for TextView with spannablestring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.