HtmlTextView


Source link: https://github.com/sufficientlysecure/html-textview

HtmlTextView for Android

HtmlTextView is an extended TextView component for Android, which can load HTML and converts it into Spannable for displaying it. It is a replacement for usage of the WebView component, which behaves strange on some Android versions, flickers while loading, etc.

The library also includes a workaround to prevent TextView from crashing on specific Android versions and the possibility to load images from local drawables folder or from the Internet.

This library is kept tiny without external dependencies. I am using it to provide Help/About Activities in my apps.

How to import

Add this to your build.gradle:

repositories {

  jcenter() 
}
  dependencies {

  compile 'org.sufficientlysecure:html-textview:3.5' 
}
 

Example

<org.sufficientlysecure.htmltextview.HtmlTextView

 android:id="@+id/html_text"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:textAppearance="@android:style/TextAppearance.Small" />
HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);
  // loads html from string and displays cat_pic.png from the app's drawable folder htmlTextView.setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>",
  new HtmlResImageGetter(htmlTextView));

or

HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);
  // loads html from string and displays cat_pic.png from the app's assets folder htmlTextView.setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>",
  new HtmlAssetsImageGetter(htmlTextView));

or

HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);
  // loads html from string and displays http://www.example.com/cat_pic.png from the Internet htmlTextView.setHtml("<h2>Hello wold</h2><img src=\"http://www.example.com/cat_pic.png\"/>",
  new HtmlHttpImageGetter(htmlTextView));

or

HtmlTextView htmlTextView = (HtmlTextView) view.findViewById(R.id.html_text);
  // loads html from raw resource, i.e., a html file in res/raw/, // this allows translatable resource (e.g., res/raw-de/ for german) htmlTextView.setHtml(R.raw.help, new HtmlHttpImageGetter(htmlTextView));

Supported HTML tags

Tags supported by Android ( history of Html class)

Extended support by HtmlTextView

  • <ul>
  • <ol>
  • <li>
  • <code>
  • <center>
  • <strike>

Support for HTML tables

HtmlTextView now supports HTML tables (to a limited extent) by condensing the text into a link which developers are able to render in a native WebView. To take advantage of the feature you'll need to:

  1. implement a ClickableTableSpan which provides access to the table HTML (which can be forwarded to a WebView)

  2. provide a DrawTableLinkSpan which defines what the table link should look like (i.e. text, text color, text size)

Take a look at the project's sample app for an example.

We recognize the standard table tags:

  • <table>
  • <tr>
  • <th>
  • <td>

as well as the tags extended by HtmlTextView. However, support doesn’t currently extend to tags natively supported by Android (e.g. <b>, <big>, <h1>) which means tables will not include the extra styling.

Changelog

3.5

  • Allow image compression

3.4

  • Fix textIsSelectable

3.3

  • Fix text cutting bug
  • prevent EmptyStackException when processing malformed li tags

3.2

  • Indenting entries of ordered lists the same way as of unordered lists
  • Fix OutOfMemory

3.1

  • Override handling of <ul> <ol> and <li> tags done by newer versions of Android SDK (removes empty lines between items)

3.0

  • Removed deprecated methods and classes
  • Fix out of bounds issue

2.0

  • Introduce better API

License

Apache License v2

See LICENSE for full license text.

Authors

Contributions

Feel free to fork and do pull requests. I am more than happy to merge them. Please do not introduce external dependencies.

Resources

A marquee layout library.

The T2 Encryption Library provides FIPS 140-2 compliant data-at-rest security to Android applications.

ExoPlayer's wrapper for using with TextureView.

Easy and quick weather fetching from OpenWeatherMap API for Android.

Image zoomer provides easy way to add zoom animations to you ImageButton. You can set the animation duration and reverse mode to reverse the animation.

Animation of liquid loading button.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes