Search code examples
androidhtmleclipsetextviewgraphical-layout-editor

Android: HTML in TextView using Eclipse Graphical Layout tool


I have a text view that has html inside. On the device it is rendered correctly, but in the preview it looks like plain HTML.

Is it possible to see the final result in Graphical Layout tool instead of plain HTML?

Thanks in advance!


Solution

  • So i know that a lot of time passed, but i've found a way to preview the HTML in the Layout editor in Android Studio (don't know about Eclipse though).

    So i just created a custom TextView callet HtmlTextView:

    public class HtmlTextView extends TextView {
        public HtmlTextView(Context context) {
            super(context);
        }
        public HtmlTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public HtmlTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public void setText(CharSequence text, BufferType type) {
    
            Spanned html = null;
    
            if(text != null) {
                html = Html.fromHtml(text.toString());
                super.setText(html, BufferType.SPANNABLE);
            } else {
               super.setText(text, type);
            }
        }
    }
    

    After that it's just a matter of using it:

    <com.example.app.custom.views.HtmlTextView
        android:text="@string/your_html_string"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
    

    The string resource looks like this:

    <string name="your_html_string"><![CDATA[My <b><font color=\'#E55720\'>AWESOME</font></b> html]]></string>
    

    Hope it helps someone!