Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strike html tag not rendering in EditText with TextFormatted

I am attempting to render an html string within an EditText control. Bold, italic, and underline html renders correctly, however strike through is just ignored.

Here is the EditText control, nothing fancy:

<EditText
        android:id="@+id/rich_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Widget.EditText"
        android:gravity="top"
        android:inputType="textFilter|textMultiLine|textNoSuggestions"
        android:minLines="8"
        android:textStyle="normal" />

And here is the code that is setting the html in the EditText control:

var textView = view.FindViewById<EditText> (Resource.Id.rich_text);
var html = "<b>bold test</b> <u>underline test</u> <i>italic test</i> <strike>strike test 1</strike> <del>strike test 2</del> <s>strike test 3</s>";
textView.TextFormatted = Html.FromHtml (html);

Here is a screenshot of how it displays, notice how the strike through tests aren't working.

enter image description here

Any ideas what I'm doing wrong?

like image 559
Justin Avatar asked Dec 20 '25 08:12

Justin


1 Answers

Here is how I solved the issue. I created an implemented of ITagHandler:

public class HtmlTagHandler : Object, Html.ITagHandler {
        public void HandleTag (bool opening, string tag, IEditable output, IXMLReader xmlReader) {  
            if (tag == "strike" || tag == "s" || tag == "del") {
                var text = output as SpannableStringBuilder;
                if (opening)
                    Start (text, new Strike ());
                else
                    End (text, Class.FromType (typeof(Strike)), new StrikethroughSpan ());
            }
        }

        private static void Start (SpannableStringBuilder text, Object mark) {
            var length = text.Length ();
            text.SetSpan (mark, length, length, SpanTypes.MarkMark);
        }

        private static void End (SpannableStringBuilder text, Class kind, Object newSpan) {
            var length = text.Length ();
            var span = GetLast (text, kind);
            var where = text.GetSpanStart (span);
            text.RemoveSpan (span);
            if (where != length)
                text.SetSpan (newSpan, where, length, SpanTypes.ExclusiveExclusive);
        }

        private static Object GetLast (ISpanned text, Class kind) {
            var length = text.Length ();
            var spans = text.GetSpans (0, length, kind);
            return spans.Length > 0 ? spans.Last () : null;
        }
    }

    class Strike : Object {
    }

This can be called like so:

public static ISpanned ToHtml (this string html) {
            return Html.FromHtml (html ?? string.Empty, null, new HtmlTagHandler ());
        }

And here is how it looks:

enter image description here

like image 69
Justin Avatar answered Dec 21 '25 21:12

Justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!