Text in Android textview hyperlink, color, font

Source: Internet
Author: User

TextView is used to display the text, sometimes need to textview in the individual words set as hyperlinks, or set the color of individual words, fonts, etc., it is necessary to use the Spannable object, you can use the Spannable object to achieve the above settings.

Activity code:

Importandroid.app.Activity; ImportAndroid.graphics.Color; ImportAndroid.os.Bundle; Importandroid.text.Spannable; Importandroid.text.SpannableString; Importandroid.text.Spanned; ImportAndroid.text.method.LinkMovementMethod; ImportAndroid.text.style.BackgroundColorSpan; ImportAndroid.text.style.ForegroundColorSpan; ImportAndroid.text.style.StyleSpan; ImportAndroid.text.style.URLSpan; ImportAndroid.widget.TextView;  Public classTextviewlinkactivityextendsActivity {TextView mytextview; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);           Setcontentview (R.layout.main); Mytextview= (TextView) This. Findviewbyid (R.id.mytextview); //Create a Spannablestring objectSpannablestring SP =NewSpannablestring ("This sentence has Baidu hyperlinks, there are highlighting, so, or so, there are italic."); //Set up HyperlinksSp.setspan (NewUrlspan ("http://www.baidu.com"), 5, 7, spanned.span_exclusive_exclusive); //Set highlight style oneSp.setspan (NewBackgroundcolorspan (color.red), 17, 19, spannable.span_exclusive_exclusive); //Set highlight style twoSp.setspan (NewForegroundcolorspan (Color.yellow), 20,24, spannable.span_exclusive_inclusive); //Set ItalicSp.setspan (NewStylespan (Android.graphics.Typeface.BOLD_ITALIC), 27, 29, spannable.span_exclusive_inclusive); //spannablestring Object set to TextViewMytextview.settext (SP); //set TextView to clickMytextview.setmovementmethod (Linkmovementmethod.getinstance ()); }   }

Text in Android textview hyperlink, color, font

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.