Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying "strikethrough" on a section of TextView text

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.

like image 542
RMS2 Avatar asked Nov 29 '10 06:11

RMS2


People also ask

How do you strikethrough text in TextView?

If you want to show a strike-through text you can do it programming using PaintFlags. You can set paint flags Paint. STRIKE_THRU_TEXT_FLAG to a TextView and it will add a strike-through to the text. TextView textView = (TextView) findViewById(R.

How do you add a line in TextView?

If you just want to add a line break in the form: textView. setText(string1+System. getProperty ("line. separator")+string2); then it works a treat, thank you!

How do I make TextView bold in specific text?

android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”. If you want to use bold and italic. Use pipeline symbol “|” .


1 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); 
like image 96
Suresh Avatar answered Sep 28 '22 02:09

Suresh