The strikethrough effect can be really useful in many situations. You can use it when you want to show a discount price, when you want to mark a wrong word or in many other situations. We all know that feature from software products like Word, Evernote, Sublime, etc. But how can we do the same thing with our simple TextView?
There are two different ways to do that, based on the desired result.
If you want to have a beautiful line going through the whole length of your text, then use the code down below:
TextView myTextView = (TextView) findViewById(R.id.my_text_view); myTextView.setText("Test test test"); myTextView.setPaintFlags(myTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
Obviously, the key part is in the setPaintFlags() method. This method modifies something called TextPaint. This object is an extension of the Paint class. It actually adds more functionality to it and it holds information about how to draw text, geometries, etc.
The other way to achieve the strikethrough effect is simpler than the one above. We can use the string resource file to define the desired text and add some HTML tags to it. Like the one down below:
<string name="your_string_name">Hello <strike>World</strike>!!!!</string>
Feel free to share, comment & give your opinion on the topic!
And only if you really REALLY liked the article, you can buy me a cup of coffee! Otherwise, don’t do it! Donate $1