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


1. StrikeThrough Using strike




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.


        textview2.paintFlags = textview2.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        textview2.text = "2. StrikeThrough Using Paint Flags"

3.1 SpannableString – TextView StrikeThrough Example

android textview strikethrough

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


val content1 = "3.1 StrikeThrough Using SpannableString"
        val spannableString1 = SpannableString(content1)
        spannableString1.setSpan(StrikethroughSpan(),0,content1.length,0)
        textview31.text = spannableString1

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.


val content2 = "3.2 StrikeThrough Using SpannableString"
        val spannableString2 = SpannableString(content2)
        spannableString2.setSpan(StrikethroughSpan(),4,17,0)
        textview32.text = spannableString2

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


textview.paintFlags = textview.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()

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)

 

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

build.gradle(Module:App)

 

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.androidride.textviewstrikethroughex_kotlin"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

colors.xml




    #008577
    #00574B
    #D81B60


strings.xml



    TextView StrikeThrough Ex
    1. StrikeThrough Using strike


styles.xml




    
    



activity_main.xml




    

    

    

    

    

    

    

        

        
    



strikethrough_layerlist.xml



        
            
                
            
        


strikethrough_shape.xml




    
    


 

MainActivity.kt

 

package com.androidride.textviewstrikethroughex_kotlin

import android.graphics.Paint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableString
import android.text.style.StrikethroughSpan
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        textview2.paintFlags = textview2.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        textview2.text = "2. StrikeThrough Using Paint Flags"

        val content1 = "3.1 StrikeThrough Using SpannableString"
        val spannableString1 = SpannableString(content1)
        spannableString1.setSpan(StrikethroughSpan(),0,content1.length,0)
        textview31.text = spannableString1

        val content2 = "3.2 StrikeThrough Using SpannableString"
        val spannableString2 = SpannableString(content2)
        spannableString2.setSpan(StrikethroughSpan(),4,17,0)
        textview32.text = spannableString2


    }
}

Let's run it.

Android TextView StrikeThrough XML

If you found this article helpful, please share it.

Please disable your adblocker or whitelist this site!