Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.YuanWenHai:html-textview:1.6'
}
dependencies {
implementation("com.github.YuanWenHai:html-textview:1.6")
}
<dependency>
<groupId>com.github.YuanWenHai</groupId>
<artifactId>html-textview</artifactId>
<version>1.6</version>
</dependency>
libraryDependencies += "com.github.YuanWenHai" % "html-textview" % "1.6"
:dependencies [[com.github.YuanWenHai/html-textview "1.6"]]
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.
Add this to your build.gradle:
repositories {
jcenter()
}
dependencies {
compile 'org.sufficientlysecure:html-textview:1.6'
}
<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\"/>", new LocalImageGetter());
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\"/>", new RemoteImageGetter());
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, new RemoteImageGetter());
<p>
<div>
handled exactly like <p>
<br>
<b>
<i>
<strong>
(bug on some Android versions: generates italic)<em>
(bug on some Android versions: generates bold)<u>
<tt>
<dfn>
<sub>
<sup>
<blockquote>
<cite>
<big>
<small>
<font size="..." color="..." face="...">
<h1>
, <h2>
, <h3>
, <h4>
, <h5>
, <h6>
<a href="...">
<img src="...">
<ul>
<ol>
<li>
<code>
<center>
<strike>
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:
implement a ClickableTableSpan
which provides access to the table HTML (which can be forwarded to a WebView)
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.
Apache License v2
See LICENSE for full license text.
Feel free to fork and do pull requests. I am more than happy to merge them. Please do not introduce external dependencies.