Convert Figma logo to code with AI

SufficientlySecure logohtml-textview

TextView to display simple HTML

2,555
502
2,555
38

Top Related Projects

2,740

Android markdown library (no WebView)

TextView to display simple HTML

Android平台下的富文本解析器,支持Html和Markdown

RichEditor for Android is a beautiful Rich Text WYSIWYG Editor for Android.

Quick Overview

HTML-TextView is an Android library that allows rendering of HTML content in a TextView. It provides support for various HTML tags and CSS styles, making it easier to display rich text content in Android applications without the need for a WebView.

Pros

  • Lightweight alternative to WebView for displaying HTML content
  • Supports a wide range of HTML tags and CSS styles
  • Customizable rendering options and text appearance
  • Actively maintained and regularly updated

Cons

  • Limited support for complex HTML layouts and advanced CSS features
  • May have performance issues with very large HTML content
  • Requires manual handling of clickable links and images
  • Some HTML elements may not render exactly as they would in a web browser

Code Examples

  1. Basic usage:
HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setHtml("<h1>Hello, World!</h1><p>This is <b>bold</b> text.</p>");
  1. Customizing link color:
HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setLinkTextColor(Color.BLUE);
htmlTextView.setHtml("<p>Visit our <a href='https://example.com'>website</a>.</p>");
  1. Handling image loading:
HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setImageGetter(new HtmlHttpImageGetter(htmlTextView));
htmlTextView.setHtml("<p>Here's an image: <img src='https://example.com/image.jpg' /></p>");

Getting Started

  1. Add the dependency to your app's build.gradle file:
dependencies {
    implementation 'org.sufficientlysecure:html-textview:4.0'
}
  1. Add the HtmlTextView to your layout XML:
<org.sufficientlysecure.htmltextview.HtmlTextView
    android:id="@+id/html_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  1. Use the HtmlTextView in your Activity or Fragment:
HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setHtml("<h1>Hello, World!</h1><p>This is <b>HTML</b> content.</p>");

Competitor Comparisons

2,740

Android markdown library (no WebView)

Pros of Markwon

  • More comprehensive Markdown support, including tables, task lists, and LaTeX
  • Highly customizable with plugins and theme options
  • Active development and regular updates

Cons of Markwon

  • Larger library size and potentially higher memory footprint
  • Steeper learning curve due to more complex API
  • May be overkill for simple HTML rendering needs

Code Comparison

html-textview:

HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setHtml("<h1>Hello, World!</h1>");

Markwon:

val markwon = Markwon.create(context)
val textView = findViewById<TextView>(R.id.markdown_view)
markwon.setMarkdown(textView, "# Hello, World!")

Summary

Markwon offers more advanced Markdown rendering capabilities and customization options, making it suitable for complex content display. However, it comes with a larger footprint and potentially more complex implementation. html-textview is simpler and lighter, focusing primarily on basic HTML rendering, which may be sufficient for less demanding use cases. The choice between the two depends on the specific requirements of the project, such as the complexity of the content to be displayed and the desired level of customization.

TextView to display simple HTML

Pros of html-textview

  • Renders HTML content in Android TextView
  • Supports basic HTML tags and CSS styling
  • Lightweight and easy to integrate

Cons of html-textview

  • Limited support for complex HTML structures
  • May not handle all CSS properties accurately
  • Performance may degrade with large HTML content

Code Comparison

Both repositories appear to be the same project, so there isn't a meaningful code comparison to make. The repository html-textview is likely a duplicate or mirror of the original project. Here's a sample of the core functionality from the main HtmlTextView class:

public class HtmlTextView extends TextView {
    public void setHtml(String html) {
        setText(HtmlFormatter.formatHtml(html));
    }
}

This code snippet demonstrates the primary method for setting HTML content in the TextView. The HtmlFormatter class is responsible for parsing and formatting the HTML string before it's displayed.

Summary

Since both repositories appear to be the same project, there aren't distinct differences to compare. The html-textview library provides a useful solution for displaying HTML content in Android TextViews, but it has limitations in terms of complex HTML rendering and CSS support. It's best suited for simple HTML content and basic styling needs in Android applications.

Android平台下的富文本解析器,支持Html和Markdown

Pros of RichText

  • More comprehensive feature set, including support for images, tables, and custom tags
  • Better performance for complex HTML rendering
  • Active development with frequent updates and bug fixes

Cons of RichText

  • Larger library size, which may impact app size
  • Steeper learning curve due to more complex API
  • May be overkill for simple HTML rendering needs

Code Comparison

html-textview:

HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setHtml("<p>Hello <b>World</b></p>");

RichText:

RichText.fromHtml("<p>Hello <b>World</b></p>")
    .into(textView);

Both libraries offer simple ways to render HTML content in Android TextViews. html-textview provides a custom TextView subclass, while RichText uses a builder pattern to configure and apply HTML content to existing TextViews.

RichText offers more advanced features and customization options, making it suitable for complex HTML rendering tasks. However, html-textview may be preferable for simpler use cases or when minimizing app size is a priority.

RichEditor for Android is a beautiful Rich Text WYSIWYG Editor for Android.

Pros of richeditor-android

  • Offers a rich text editing experience with formatting options like bold, italic, underline, and more
  • Supports image insertion and manipulation within the editor
  • Provides a WYSIWYG interface for easier content creation

Cons of richeditor-android

  • More complex implementation and potentially larger codebase
  • May have a steeper learning curve for developers
  • Could be overkill for simple text display needs

Code Comparison

html-textview:

HtmlTextView htmlTextView = findViewById(R.id.html_text);
htmlTextView.setHtml("<p>Hello, <b>world!</b></p>");

richeditor-android:

RichEditor editor = findViewById(R.id.editor);
editor.setEditorHeight(200);
editor.setEditorFontSize(22);
editor.setEditorFontColor(Color.RED);
editor.setPlaceholder("Insert text here...");

Summary

html-textview is a lightweight solution for displaying HTML content in Android TextViews, while richeditor-android provides a full-featured rich text editor. html-textview is simpler to implement and use, making it ideal for basic HTML rendering. richeditor-android offers more advanced editing capabilities but comes with increased complexity. The choice between the two depends on whether you need simple HTML display or rich text editing functionality in your Android application.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Project stopped

This project has been stopped. 4.0 is the last release.

Feel free to fork this project and take over maintaining.

HtmlTextView for Android

HtmlTextView is an extended TextView component for Android, which can load very simple HTML by converting it into Android Spannables for viewing.

In addition to a small set of HTML tags, the library allows to load images from the local drawables folder or from the Internet.

This library is kept tiny without external dependencies.

How to import

Add this to your build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'org.sufficientlysecure:html-textview:4.0'
}

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));

or

<TextView
    android:id="@+id/html_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="@android:style/TextAppearance.Small" />
TextView htmlTextView = (TextView) view.findViewById(R.id.html_text);

// loads html from string and displays cat_pic.png from the app's drawable folder
Spanned formattedHtml = HtmlFormatter.formatHtml(new HtmlFormatterBuilder().setHtml("<h2>Hello wold</h2><ul><li>cats</li><li>dogs</li></ul><img src=\"cat_pic\"/>").setImageGetter(new HtmlResImageGetter(htmlTextView.getContext())));
htmlTextView.setText(formattedHtml);

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.

Support for A tag click listener

textView.setOnClickATagListener(new OnClickATagListener() {

    @Override
    public void onClick(View widget, @Nullable String href) {
        Toast.makeText(MainActivity.this, href, Toast.LENGTH_SHORT).show();
    }
});

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

4.0

3.9

  • Add A tag click listener

3.8

  • Add HtmlFormatter for use with standard TextView

3.7

  • Fix #166
  • Migrated to AndroidX

3.6

  • Improve and fix indentions

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.