3

This in not a duplicate question, i found some question but they were not having satisfied answers, answers were not available for multiline textviews.

I have already checked this below link but they were not enough useful: Is there an easy way to strike through text in an app widget?

I want to create a line across the center of a TextView (striked TextView), but I want to do it in XML layout, not in the Java code.

I have already achieved this result through the use of JAVA code as such:

myTextView.setPaintFlags(myTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

I would like the same result, but in the below XML code:

<TextView
    android:id="@+id/myTextView" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 

    />

enter image description here

Thanks again.

Community
  • 1
  • 1
Androider
  • 3,833
  • 2
  • 14
  • 24

3 Answers3

3
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="XYZ"
        android:background="@drawable/line"/>

line.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">

    <stroke android:width="1dp"
        android:color="@android:color/white"/>

</shape>

Try this !

EDIT

The above code will work only for single line text. For Multi line text use below code :

<string name="strike_text">
        <strike>sample TextView \nSecond line of TextView</strike>
</string>

And use it as

android:text="@string/strike_text"
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
1

Easiest way would be to use an image.

create image with horizontal line and transparent background and set it as background of TextView

android:background="@drawable/strike_through"

Or

Create an xml file which will draw a line and set it as background of your TextView

<shape android:shape="line">
     <stroke android:width="2dp" android:color="#ffffff" />
</shape>
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

The way to do this in xml is to create a drawable of the strike and set it as the background of the TextView. An example of the drawable would be:

<item android:state_pressed="false">
    <shape android:shape="line">
         <stroke android:width="2dp" android:color="#ffffff" />
    </shape>
</item>
yedidyak
  • 1,964
  • 13
  • 26