Android: Use SpannableString to set rich display effects for TextView

Source: Internet
Author: User

Android: Use SpannableString to set rich display effects for TextView

In the process of using TextView, some texts in a string of text are sometimes needed for special display effect processing, such as bold, color changing, highlighted logo, and hyperlink, we can use multiple textviews to piece together, but if the content to be specially processed is in the middle of the entire text, it is too troublesome and too Low to piece together multiple textviews, the SpannableString class provided by Android can solve this problem well. The SpannableString can be combined with various classes ending with Span to provide a wide variety of display effects.


This document describes a SpannableUtils tool Class Based on your work conditions for future use. java code:

/*** Spannable tool class, it is used to set the foreground color, background color, Typeface, bold, italic, font size, hyperlink, strikethrough, underline, and upper/lower mark of a text ***/public class SpannableUtils {private SpannableUtils () {}/*** change the font size of a text segment in the string * setTextSize ("", 24, 0, 2) = null; * setTextSize (null, 24, 0, 2) = null; * setTextSize ("abc",-2, 0) = 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 | startIndex <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 (content) | 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> = 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 (content) | 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 (TextUtils. 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 (TextUtils. 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 the text hyperlink * @ param content the text to be processed * @ param startIndex * @ param endIndex the start and end indexes of the strings to be processed in the text to be processed * @ param url link address corresponding to the text, note the format: * (1) the phone starts with "tel:", for example, "tel: 02355692427" * (2) the mail starts with "mailto:", for example, "mailto: zmywly8866@gmail.com "* (3) sms with" sms: "headers, such as" sms: 02355692427 "* (4) mms with" mms: "headers, such as" mms: 02355692427 "* (5) map headers with" geo: ", for example," geo: 68.426537, 68.123456 "* (6) network headers with" http, for example, "http://www.google.com" **/public static SpannableString setTextURL (String content, int startIndex, int endIndex, String url) {if (TextUtils. isEmpty (content) | startIndex <0 | endIndex> = 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 using this tool class:

Download: Spannable instance

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.