32

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.

RMS2
  • 707
  • 3
  • 8
  • 19

6 Answers6

95

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
  • 9,495
  • 14
  • 49
  • 63
  • 3
    Thanks, but this looks like it applies to the whole text. I just need to strikethrough a portion of the text - any words surrounded by hyphens, eg. -this- or -that-. – RMS2 Nov 29 '10 at 07:42
  • RMS2 have u find any solution for ur question ? please post it . – Sandip Lawate Sep 05 '14 at 12:01
  • thanks for replay but am getting response like this 5000 10000 50% off plan to set using Html.fromhtml(string) how can i handle please help am having only one textview – Harsha Oct 16 '15 at 10:03
38

@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.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
1

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);
    }
}
....
Marcelo
  • 4,580
  • 7
  • 29
  • 46
0

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

Wayne
  • 1,075
  • 1
  • 10
  • 21
0

Most of the applications we work in are going to use text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text, we essentially have some functions available for the SpannableStringBuilder class. For example, after instantiating a Builder instance we can use the build methods to append some bold text:

 textView.text =buildSpannedString {
        strikeThrough {
            append(
                value ?: ""
            )
        }
    }
Arunachalam k
  • 734
  • 1
  • 7
  • 20
0

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 ;(

Maciej Łopaciński
  • 1,234
  • 12
  • 12