Skip to content

lvke88/html-textview

 
 

Repository files navigation

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:1.1'
}

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 text = (HtmlTextView) view.findViewById(R.id.html_text);

// loads html from string and displays cat_pic.png from the app's drawable folder
text.setHtmlFromString("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>", true);

or

HtmlTextView text = (HtmlTextView) view.findViewById(R.id.html_text);

// loads html from string and displays http://www.example.com/cat_pic.png from the Internet
text.setHtmlFromString("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"http://www.example.com/cat_pic.png\"/>", false);

or

HtmlTextView text = (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)
text.setHtmlFromRawResource(this, R.raw.help, true);

Supported HTML tags

Tags supported by Android (history of Html class)

Extended support by HtmlTextView

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

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.

About

TextView to load HTML

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%