Display text containing HTML tags in Android textview

Source: Internet
Author: User

Two small instances:

1, the service side returned with the HTML tag text, in the TextView display in the time, can correct the line.

2, need to display the download hyperlink, to text instead, hide:

The implementation results are as follows:


The code is as follows:

Package Com.android.study;import Android.app.activity;import Android.os.bundle;import android.text.Html;import Android.text.method.linkmovementmethod;import Android.widget.textview;public class FourthActivity extends Activity { Private TextView textview;private TextView texturl;private TextView texturl2;//HTML format wrap private static String msghtml = "1 , complete the task before the end of the task, and upload the data. &LT;BR/>2, Mission content: 2 consecutive days to reach 10000 steps. <br/>3, you can see the progress of your task at the bottom of the progress display. ";//hyperlink private static string html =" My client: <a href= ' http://www.baidu.com ' > click Download </a> ";p rivate static string HTML2 = "My client,:";p rivate static String html22 = "<a href=\" "+" http://www.baidu.com "+" \ ">" + "click to download" + "</a> "; Charsequence charsequence = html.fromhtml (Html), @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_texttest); init ();} private void Init () {///Textview.setautolinkmask (linkify.all); TextView = (TextView) Findviewbyid (R.id.textview); Texturl = (TExtview) Findviewbyid (r.id.texturl); textUrl2 = (TextView) Findviewbyid (R.ID.TEXTURL2);/** * HTML format Wrap */ Textview.settext (html.fromhtml (msghtml));/** * Hyperlink: Law one */texturl.settext (charsequence); Texturl.setmovementmethod ( Linkmovementmethod.getinstance ());/** * Hyperlink: Law two */texturl2.settext (HTML2); Texturl2.append (html.fromhtml (html22)); Texturl2.setmovementmethod (Linkmovementmethod.getinstance ());}}

The form of hyperlinks can both be achieved in this demo.

But the French one in my project code is not used successfully, replaced by law two can be.

There is a message to know the situation oh.

Display text containing HTML tags in Android textview

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.