1

My requirement:

I want strike the particular strings in my textview.

Please take a look at the image below. In project 2 box task 8,task 5,task 1,task 3 are striked.

enter image description here

What I am tried:

After refer this SO question,

I can strike whole text view. (but I can`t strike particular string wise)

 TextView tv = (TextView) findViewById(R.id.mytext);
 tv.setText("This is strike-thru");
 tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

My problem:

1.I want to strike the particular strings only.

2.Not entire textbox.

Community
  • 1
  • 1
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159

2 Answers2

4

see this link , you can see all of text styling there.
you need to use spannableString . just choose style and specify the start and end index of your string to get style.

    SpannableString string = new SpannableString("your text here");

    string.setSpan(new StrikethroughSpan(), 0, string.length(), 0); 

    textView.setText(string);
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
MHP
  • 2,613
  • 1
  • 16
  • 26
1

Try this to add a Striked-Through text programmatically

tv.setText(Html.fromHtml("This is <del>strike-thru</del>."));

OR

if you want to add it through your xml file you can do it as follows

<string name="line"> This is <strike> strike-thru</strike> </string>

And in your Textview just add:

<TextView 
        ...
         android:text="@string/line"
 />

UPDATE

If <del> is not working, try using <strike> like,

tv.setText(Html.fromHtml("This is <strike>strike-thru</strike>."));
Lal
  • 14,726
  • 4
  • 45
  • 70