Android TextView StrikeThrough XML

You might have seen strikethrough text in websites or apps.

Strikethrough used to show the old price or real price with the selling price.

It makes us think we get more discount, so it increases the chances to buy the product.

So If you are making an e-commerce app it’s better to know how to strikethrough a text.



Here, I have discussed 4 Android TextView StrikeThrough XML and 2 Java/Kotlin examples.


Then Let’s code.

1. Using Strike – Android TextView StrikeThrough XML example

Android TextView Strikethrough

Like Html, <Strike> tag easily creates a horizontal line through our TextView content. Create a string element in strings.xml and place text which you want to get strikethrough between <strike> opening tag and closing tag.

strings.xml

2. Using Paint Flags – TextView StrikeThrough Example

Android make TextView StrikeThrough

Paint class provides STRIKE_THRU_TEXT_FLAG named flag. It creates strikethrough for TextView easily.

3.1 SpannableString – TextView StrikeThrough Example

android textview strikethrough

Applying StrikethroughSpan() with setSpan method of SpannableString, We can also create StrikeThrough.

3.2 Using SpannableString – TextView StrikeThrough Example

We can control strikethrough where to appear in a text. For that, provide start and endpoints of the text. Here I StrikeThrough the content “StrikeThrough” using 4 as the start point and 17 as the endpoint.

4. Using shape drawable – Android TextView StrikeThrough XML

Android textview strikethrough color

Using Shape drawable, create a horizontal line and put that line middle of your text.

strikethrough_shape.xml

put strikethrough_shape.xml as TextView background attribute value.

5. Using LayerList – Android TextView StrikeThrough XML

android strikethrough textview

We can create a horizontal line using layerlist drawable also. Just like the above example, put this also as TextView’s background value.

strikethrough_layerlist

put strikethrough_layerlist.xml as TextView’s background value.

6. Using RelativeLayout and View – Android TextView Strikethrough XML example

Android textview strikethrough color

RelativeLayout positions its element relative to each other. So let’s create a horizontal line using view and position it above-center of the TextView.

How To Remove TextView StrikeThrough

Use inv() method with Paint.STRIKE_THRU_TEXT_FLAG. Java users, use this ‘~’ symbol with Paint.STRIKE_THRU_TEXT_FLAG. Just like below

Create an Android Studio project

Let’s create a project with the above examples.

Open Android Studio and start a new Android Studio project.

Application name : TextView StrikeThrough Ex.

Company domain: androidride.example.com

Check to include Kotlin support.


Select minimum SDK:API 15 – Android 4.0.3 (Ice Cream Sandwich) and click Next.

Next dialog, Select Empty Activity and click Next.

Activity Name: MainActivity
Check Generate layout file

Layout Name: activity_main

Click Finish.

build.gradle(Project: TextViewStrikeThroughEx)

build.gradle(Module:App)

colors.xml

strings.xml

styles.xml

activity_main.xml

strikethrough_layerlist.xml

strikethrough_shape.xml

MainActivity.kt

Let’s run it.

Android TextView StrikeThrough XML

If you found this article helpful, please share it.