Remove underline in hyperlink text in Android textview

Source: Internet
Author: User

As a J2EE developer, I decided to write a blog today because I was inspired by the prevalence of plagiarism and reprinting on the Internet! It mainly records some experiences in the development process and small programming skills. Of course, I finally made up my mind to start blogging because of a problem that has been plagued by these days. Recently, I started Android development and want to edit text with hyperlinks in textview. It's actually quite simple. It can be implemented using spannablestring, But like other languages, the default hyperlink text in Android is blue and underlined, which is very ugly for us! So I went online to find a solution and asked Gu and Bai, who found that they provided almost the same article, no matter which website they were on .... Ah, let's explore it by yourself.

The following is a simple demo for your reference.

1. The first is the layout file, which is a simple textview.

<Textview

Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""
Android: textcolor = "#000000"
Android: gravity = "left"
/>

2. dynamically set content in Java code

Textview TV = (textview) findviewbyid (R. Id. textview );

Spannablestring text = new spannablestrig ("Baidu ");

Text. setspan (New urlspannounderline ("http://www.baidu.com"), spanned. span_exclusive_exclusive );

TV. settext (text );

TV. setmovementmethod (linkmovementmethod. getinstance ());

3. The custom urlspan inherits the urlspan and removes the underline.

Import Android. Graphics. color;
Import Android. Text. textpaint;
Import Android. Text. style. urlspan;

// Remove the underline from the custom urlspan
Public class urlspannounderline extends urlspan {
Public urlspannounderline (string URL ){
Super (URL );
}

@ Override
Public void updatedrawstate (textpaint DS ){
Super. updatedrawstate (DS );
DS. setunderlinetext (false );
DS. setcolor (color. Black );
}
}

Note: Do not set autolink in the layout file.

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.