Home » Android » android – Specifying "strikethrough" on a section of TextView text

android – Specifying "strikethrough" on a section of TextView text

Posted by: admin April 23, 2020 Leave a comment


I have a block of text coming from a webservice, and depending on some tags which I have predefined, I want to style the text before setting it to my TextView. For bold, italics, and underline, I was able to do this easily with the replaceAll command:

PageText = PageText.replaceAll("\*([a-zA-Z0-9]+)\*", "<b>$1</b>");
        PageText = PageText.replaceAll("=([a-zA-Z0-9]+)=", "<i>$1</i>");            
        PageText = PageText.replaceAll("_([a-zA-Z0-9]+)_", "<u>$1</u>");
txtPage.setText(Html.fromHtml(PageText), TextView.BufferType.SPANNABLE);

So, to bold a word, surround it with *’s, for italics, surround with _.

But, for strikethrough, Html.fromHtml does not support the “strike” tag, so it can’t be done this same way. I’ve seen examples of using Spannable to set the styling on one section of text, but it requires positional numbers. So, I guess I could loop through the text, searching for – (the tag to represent the strike), then searching for the next one, spanning the text in between, and repeating for all such strings. It will end up being 10 lines of looping code as opposed to 1 for the others, so I’m wondering if there is a more elegant solution out there.

How to&Answers:

If it is just TextView you can strike through using paint flags

TextView tv=(TextView) v.findViewById(android.R.id.text1);
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);


@Suresh solution works if you want to strikethrough the entire TextView but if you want to strikethrough only some portions of the text then use the code below.

    tvMRP.setText(text, TextView.BufferType.SPANNABLE);
    Spannable spannable = (Spannable) tvMRP.getText();
    spannable.setSpan(new StrikethroughSpan(), 3, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Here text is the text which we want out TextView to display, 3 is the no. of characters (starting from 0) from where the strikethrough will start.


Here is a solution using a custom TagHandler you have to pass to Html.fromHtml().

Android: How to use the Html.TagHandler?


You can do it with a custom TagHandler such as the one on this SO question:

Spanned parsed = Html.fromHtml(PageText, null, new MyHtmlTagHandler());

And the TagHandler implements the methods:

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


Are you sure Html.fromHtml doesn’t support <strike>? It’s listed in this Commonsware blog post


It looks like is not really supported, at least it does not work on Android 3.1.

@RMS2 if text is small you can split it into two or three separate text views and apply flag only to the one which you want, not perfect for long texts ;(