24

I have started to make an application similar to concept of "To do task manager", in which i need to make a textview cancelable, like, when user clicks on done image, whole text in the view will be cancelled.

Can anyone please guide me on, how to achieve this mechanism ??

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • I think this answer will help you: http://stackoverflow.com/a/4752292/617044 – Bill Mote Apr 12 '13 at 11:36
  • possible duplicate of [Is there an easy way to strike through text in an app widget?](http://stackoverflow.com/questions/3881553/is-there-an-easy-way-to-strike-through-text-in-an-app-widget) – Bill Mote Apr 12 '13 at 11:37

3 Answers3

67

This can help you.

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

You can use it in your code,however necessary.

Josyula Krishna
  • 1,075
  • 1
  • 11
  • 22
2

In Kotlin

tv.text = "This is text"
tv.paintFlags = tv.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
Nux
  • 5,890
  • 13
  • 48
  • 74
  • Thanks nice decision `tv.apply { text = "This is text" paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG }` – Black_Zerg Apr 20 '21 at 11:06
1

If you want to strikethrough only part of the TextView text you can use SpannableStringBuilder. Note that it is tv.text = ssb not tv.text = ssb.toString()

val text = "tv.text = ssb.toString()"
val ssb = SpannableStringBuilder(text)
ssb.setSpan(
    StrikethroughSpan(),
    13, // start of the span (inclusive)
    24, // end of the span (exclusive)
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
tv.text = ssb

When you run it, it comes out like this:

tv.text = ssb.toString()
Joonsoo
  • 788
  • 1
  • 13
  • 15