How can I define middle-lined(strikethrough) text in an Android layout xml file?
6 Answers
To strike through, you can use a background image to create the strikethrough effect:
 android:background="@drawable/strike_through"
Where strike_through drawable is a 9-patch image that keeps a line through the middle. This is the easiest way to implement it.
or you can do it programatically as this.
TextView t = (TextView) findViewById(R.id.text);
t.setText("Text here");
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
- 4,189
 - 4
 - 31
 - 53
 
- 
                    Can I change the color of this middle line ? – Adham Jun 02 '12 at 15:18
 - 
                    for the xml implementation you just need to change the image that you use for background. for the programmatic method i am not sure if it is possible. – Anurag Ramdasan Jun 02 '12 at 15:31
 - 
                    how do you do this in kotlin? – Vina Dec 27 '17 at 18:07
 - 
                    where to find `strike_through` image – Allen Dec 07 '22 at 09:18
 
6 Amazing Ways - Android TextView Strikethrough XML & Kotlin/Java examples
Screenshot - Android TextView Strikethrough XML & Kotlin/java Example -
Using strike element.
strings.xml
<string name="strike_text">1. <strike>StrikeThrough</strike> Using strike</string>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/strike_text"
/>    
Using STRIKE_THRU_TEXT_FLAG
Kotlin
textview2.paintFlags = textview2.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG textview2.text = "2. StrikeThrough Using Paint FlagsJava
textview2.setPaintFlags(textview2.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG); textview2.setText("2. StrikeThrough Using Paint Flags");Using SpannableString
Kotlin
val content1 = "3.1 StrikeThrough Using SpannableString"
        val spannableString1 = SpannableString(content1)
        spannableString1.setSpan(StrikethroughSpan(),0,content1.length,0)
        textview31.text = spannableString1
Java
textview2.setPaintFlags(textview2.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
    textview2.setText("2. StrikeThrough Using Paint Flags");    `
- 65
 - 8
 
- 241
 - 3
 - 4
 
If you define a BindingAdapter
@BindingAdapter("strikeThrough")
public static void strikeThrough(TextView textView, Boolean strikeThrough) {
    if (strikeThrough) {
        textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    } else {
        textView.setPaintFlags(textView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
    }
}
it's just
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
    app:strikeThrough="@{true}"
    .../>
in XML layout file.
- 229
 - 2
 - 6
 
- 
                    I wanted to use data binding and to set text to strikethrough based on ViewModel logic. This is exactly what I needed. Thanks. – zivko May 09 '23 at 10:42
 
To do it in only xml file, this is what I do:
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/text_view_original_cash_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@android:color/darker_gray"
        android:text="$36000"/>
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_centerVertical="true"
        android:layout_alignStart="@id/text_view_original_cash_amount"
        android:layout_alignEnd="@id/text_view_original_cash_amount"/>
</RelativeLayout>
Hope this helps!
- 5,187
 - 2
 - 30
 - 31
 
In kotlin it can be done as:
your_textView.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
- 498
 - 5
 - 16
 
textview.setPaintFlags(textview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
you can use the above code in activity to have strike over the text.
And to set through xml refer this
- 1
 - 1
 
- 34,573
 - 7
 - 66
 - 64
 
