4

I have searched lot. I have to display Html code in Textview. But Mostly Html.fromHtml(source) used in Textview. But this method is not supported all html tags. Only few tags supported by Html.fromHtml().

Because i need to appy all css and all html tags support.

I want to display html code in Textview without using Webview. Is there any other way to implement this functionality.

rajeshlawrance
  • 549
  • 2
  • 6
  • 25
  • best option use `webview` ... but as you said **you don't want to use webview** ... any specific reason for that?? – Maveňツ Nov 24 '16 at 13:27
  • You may need to write support for the unsupported tags in `Html.fromHtml()` if you do not want to use `WebView`. Check out [this](http://stackoverflow.com/a/3150456/1987045) and [this](http://stackoverflow.com/a/9546532/1987045) answers for more details. – rahulrvp Nov 24 '16 at 13:33
  • because webview is only for display. I want to update some textx in html editor. thats why i asked – rajeshlawrance Nov 24 '16 at 13:34
  • @rajeshlawrance are you looking for an html editor to include in your app? – rahulrvp Nov 24 '16 at 13:36
  • yes.i want to use – rajeshlawrance Nov 24 '16 at 13:39
  • Refer [this](http://stackoverflow.com/a/33691923/6005977) link. In this, suggested a library. – Ankita Shah Nov 28 '16 at 11:26
  • You cannot do it with native Android. You have to use a 3rd party library – Zoe Nov 29 '16 at 19:58
  • I have seen a nice talk in droidcon about that : https://www.youtube.com/watch?v=7GFRpP9a-eQ – Josef Korbel Dec 01 '16 at 12:53
  • @rajeshlawrance what kinds of html tags you need support in TextView? – Tapa Save Dec 02 '16 at 07:39
  • @rajeshlawrance I think you are looking at solving this problem in the wrong way mate. From your replies, it is clear that you want to manipulate the text while styling the text and layout, with CSS. Are you sure there isn't a way to do what you want to do it purely on Android without using the Android framework? You really should read the material design guidelines on this https://material.google.com/style/typography.html#typography-language-categories-reference – Mushtaq Jameel Dec 05 '16 at 05:08

5 Answers5

4

Refer this link. Use 3rd party libraries that improve native TextView and support more HTML tags. For example: HTML-TextView.

Community
  • 1
  • 1
Ankita Shah
  • 1,866
  • 16
  • 31
1

Yes you can display , but in that case you will have to implement HTMLTagHandler to render your tags (As only few tags are supported by Html.fromHtml() method) and ImageGetter to display your images. I have created a sample project, please check it here.

  • public class HtmlTagHandler implements Html.TagHandler {}
  • public final class GlideImageGetter implements Html.ImageGetter{}

You can call this as

Html.fromHtml(htmlText, new GlideImageGetter(webViewReferenceObj, 
(AppCompatActivity) mContext), new HtmlTagHandler())
Tasneem
  • 793
  • 8
  • 24
0

It is good that you are digging deep into Html.fromHtml(). But there is only this way to use html code and also for better look and layout you can wrap your string file like this.

<string><![CDATA[<html>YOUR STRING FILE</html>]></string>

Or simply you can use Webview.

Krunal Kapadiya
  • 2,853
  • 3
  • 16
  • 37
0

This link give you the correct answer for this solution without using web view and also it supports more html tags like table,image and list.

android-summernote

rajeshlawrance
  • 549
  • 2
  • 6
  • 25
-1

I found this tutorial, You can use HTML and CSS with TextView without using WebView but you have to use Html.fromHtml as below and also you should be using Spanned

Give a try!!

Android-Spannable Info Refer here

public class MainActivity extends ActionBarActivity {
    private TextView textView;
    private final String htmlWithCSS = "<!DOCTYPE html>\n" +
            "<html>\n" +
            "<head>\n" +
            "<style>\n" +
            "a1 {\n" +
            "    text-decoration: overline;\n" +
            "}\n" +
            "\n" +
            "a2 {\n" +
            "    text-decoration: line-through;\n" +
            "}\n" +
            "\n" +
            "a3 {\n" +
            "    text-decoration: underline;\n" +
            "}\n" +
            "</style>\n" +
            "</head>\n" +
            "<body>\n" +
            "\n" +
            "<a1>This is heading 1</a1>\n" +
            "<a2>This is heading 2</a2>\n" +
            "<a3>This is heading 3</a3>\n" +
            "\n" +
            "</body>\n" +
            "</html>\n";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = ((CustomTextView) findViewById(R.id.text_view));
        Spanned spanned = Html.fromHtml(htmlWithCSS);
        textView.setText(spanned);
    }
}
Mahesh G
  • 1,226
  • 4
  • 30
  • 57