Domanda

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!

È stato utile?

Soluzione

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!

Altri suggerimenti

AFAIK Android does not show the rendering of HTML in TextView in Graphical Layout tool. It will only show when you will run your application.

The TextView class already supports some basic html tags using Html.fromHtml.

Not clear enough but try this

tv.setText(Html.fromHtml(yourString), TextView.BufferType.SPANNABLE);

or try this take webview instead of TextView

webview.loadData(yourString,"text/html","utf-8");

My Eclipse Layout-Preview didn't show the strike-tags, so I extended the answer from @Arthur:

public class HtmlTextView extends TextView {
CustomHtmlTagHandler tagHandler = new CustomHtmlTagHandler();

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(), null, tagHandler);
        super.setText(html, BufferType.SPANNABLE);
    } else {
       super.setText(text, type);
    }
}
}

And this is the CustomHtmlTagHandler:

public class CustomHtmlTagHandler implements TagHandler {

public void handleTag(boolean opening, String tag, Editable output,
        XMLReader xmlReader) {
    if(tag.equalsIgnoreCase("strike") || tag.equals("s")) {
        processStrike(opening, output);
    }
}

private void processStrike(boolean opening, Editable output) {
    int len = output.length();
    if(opening) {
        output.setSpan(new StrikethroughSpan(), len, len, Spannable.SPAN_MARK_MARK);
    } else {
        Object obj = getLast(output, StrikethroughSpan.class);
        int where = output.getSpanStart(obj);

        output.removeSpan(obj);

        if (where != len) {
            output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

private Object getLast(Editable text, Class kind) {
    Object[] objs = text.getSpans(0, text.length(), kind);

    if (objs.length == 0) {
        return null;
    } else {
        for(int i = objs.length;i>0;i--) {
            if(text.getSpanFlags(objs[i-1]) == Spannable.SPAN_MARK_MARK) {
                return objs[i-1];
            }
        }
        return null;
    }
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top