Android TextView loading HTML content and loading pictures

Source: Internet
Author: User

  1. Package com.example.textviewfromhtml;
  2. Import Java.net.URL;
  3. Import android.app.Activity;
  4. Import android.graphics.drawable.Drawable;
  5. Import Android.os.Bundle;
  6. Import Android.os.StrictMode;
  7. Import android.text.Html;
  8. Import Android.text.Html.ImageGetter;
  9. Import Android.text.method.LinkMovementMethod;
  10. Import Android.text.method.ScrollingMovementMethod;
  11. Import Android.util.Log;
  12. Import Android.view.Menu;
  13. Import Android.widget.TextView;
  14. public class Mainactivity extends Activity {
  15. @Override
  16. protected void OnCreate (Bundle savedinstancestate) {
  17. Super.oncreate (savedinstancestate);
  18. struct ();
  19. Setcontentview (R.layout.activity_main);
  20. TextView TV = (TextView) Findviewbyid (r.id.txt);
  21. String html = "
  22.                 +  " span class= "tag" ><p>< a href=\ "http://www.dreamdu.com/xhtml/\" > Hyperlink HTML Get started </a> learn Html! </p><p ><font color=\ "# Aabb00\ "> color 1"   
  23.                  +  "</p><p><font  color=\ "#00bbaa \" > color 2</ p><p ><font color=\ "#aabb00 \" > color 1 "  
  24.                 +  " span class= "tag" ></p>< p><font color=\ "#00bbaa \" > color 2</p< span class= "tag" >><p>< font color=\ "#aabb00 \" > color 1 "   
  25.                  +  "</p><p><font  color=\ "#00bbaa \" > color 2</ p><p ><font color=\ "#aabb00 \" > color 1 "  
  26. + "</P><p><font color=\" #00bbaa \ "> Color 2</p ><p><font color=\ "#aabb00 \"> Color 1 "
  27. + "</P><P><FontColor=\ "#00bbaa \"> Color 2</p>< h1> title 1</h1 ><h3> Title 2 </h3>< h6> title 3</h6 ><p> greater than > Less than <</p> <p> "&NBSP;&NBSP;
  28. + "Below is the network picture </P><img src=\" http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\ "  /></body></html> ";
  29. Tv.setmovementmethod (Scrollingmovementmethod.getinstance ());//Set scrollable
  30. Tv.setmovementmethod (Linkmovementmethod.getinstance ());//Set hyperlinks to open web pages
  31. Tv.settext (html.fromhtml (Html, imggetter, null));
  32. }
  33. @Override
  34. public boolean Oncreateoptionsmenu (Menu menu) {
  35. Inflate the menu; This adds items to the action bar if it is present.
  36. Getmenuinflater (). Inflate (R.menu.activity_main, menu);
  37. return true;
  38. }
  39. The resource inside this is the URL contained in the first parameter of the fromhtml function.
  40. Imagegetter imggetter = new Html.imagegetter () {
  41. Public drawable getdrawable (String source) {
  42. LOG.I ("RG", "Source---?>>>" + source);
  43. drawable drawable = null;
  44. URL url;
  45. try {
  46. url = new URL (source);
  47. LOG.I ("RG", "url---?>>>" + URL);
  48. drawable = Drawable.createfromstream (Url.openstream (), ""); Get Network images
  49. } catch (Exception e) {
  50. E.printstacktrace ();
  51. return null;
  52. }
  53. Drawable.setbounds (0, 0, drawable.getintrinsicwidth (),
  54. Drawable.getintrinsicheight ());
  55. LOG.I ("RG", "url---?>>>" + URL);
  56. return drawable;
  57. }
  58. };
  59. public static void struct () {
  60. Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()
  61. . Detectdiskreads (). Detectdiskwrites (). Detectnetwork ()//or
  62. . Detectall ()
  63. For
  64. All
  65. Detectable
  66. Problems
  67. . Penaltylog (). build ());
  68. Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()
  69. . Detectleakedsqlliteobjects ()//probing SQLite database operations
  70. . Penaltylog ()//Print Logcat
  71. . Penaltydeath (). build ());
  72. }
  73. }

Remember to add permissions: <uses-permission android:name= "Android.permission.INTERNET"/>

The effect is as follows:

Android TextView loading HTML content and loading pictures

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.