How is TextView combined with HTML?

Source: Internet
Author: User


How is TextView combined with HTML?


Android Tutorial TextView parsing HTML example with pictures

The TextView in Android natively supports some HTML formatting tags. This includes commonly used font size color settings, text links, and so on. It is also convenient to use, just use the HTML class to convert it. Like what:

Textview.settext (html.fromhtml (str));

First, the realization of the text in the TextView have different colors

Import android.text.Html;

TextView t3 = (TextView) Findviewbyid (R.ID.TEXT3);
T3.settext (html.fromhtml ("<b>text3:</b> Text with a" + "<a href=\" http://www.google.com\ ">link< /a> "+" created in the Java source code using HTML. "));

Second, TextView display the picture in the HTML file
We know to let TextView parse and display the HTML code. can use

spanned text = html.fromhtml (source);
Tv.settext (text);

To achieve, this is easy to use.
But how do you let TextView also display images of <image> nodes in HTML?
We can see that fromhtml also has another refactoring:

Fromhtml (String source, Html.imagegetter Imagegetter, Html.taghandler Taghandler)
Implement the Imagegetter to let the Picture Show:

Imagegetter imggetter = new Html.imagegetter () {
@Override
Public drawable getdrawable (String source) {
Drawable drawable = null;
drawable = Drawable.createfrompath (source); Or fetch it from the URL
Important
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), drawable
. Getintrinsicheight ());
return drawable;
}
};

As for Taghandler, we do not need to use it here, we can pass null directly.

---------------------
public class Mainactivity extends Activity {

Private Handler Handler;
Private String html;
Private TextView TV;
Private ProgressBar Bar;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

HTML data found on the web
html = "+ "<p><a href=\" http://www.jb51.net "> Hyperlink HTML Get started </a> learn Html!</p><p><font color=\" # Aabb00\ "> Color 1"
+ "</p><p><font color=\" #00bbaa \ "> Color 2</p>+ "Below is the network picture </p></body>"
+ "Below is the network picture </p></body>
TV = (TextView) This.findviewbyid (r.id.id);
Bar = (ProgressBar) This.findviewbyid (R.id.id_bar);
Tv.setmovementmethod (Scrollingmovementmethod.getinstance ());//scrolling


Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {

if (msg.what = = 0x101) {
Bar.setvisibility (View.gone);
Tv.settext ((charsequence) msg.obj);
}
Super.handlemessage (msg);
}
};

Because downloading pictures from the internet is a time-consuming operation, you need to open new threads
Thread t = new Thread (new Runnable () {

Message msg = Message.obtain ();
@Override
public void Run () {

Bar.setvisibility (view.visible);
/**
* To implement a picture display requires a refactoring method using html.fromhtml: public static spanned
* Fromhtml (String source, Html.imagegetterimagegetter,
* Html.taghandler
* Taghandler) where Html.imagegetter is an interface, we want to implement this interface in its getdrawable
The Drawable object in the * (String source) method to return a picture.
*/

Imagegetter imagegetter = new Imagegetter () {
@Override
Public drawable getdrawable (String source) {

URL url;
Drawable drawable = null;
try {
url = new URL (source);
drawable = Drawable.createfromstream (Url.openstream (), NULL);
Drawable.setbounds (0, 0,drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());

} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}

return drawable;
}
};

Charsequence test = html.fromhtml (Html, imagegetter, NULL);
Msg.what = 0x101;
Msg.obj = test;
Handler.sendmessage (msg);
}
});

T.start ();
}



}

How is TextView combined with HTML?

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.