Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a strikethrough text?

Paint.STRIKE_THRU_TEXT_FLAG

TextView someTextView = (TextView) findViewById(R.id.some_text_view);
someTextView.setText(someString);
someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

For painting text, there are several bit flags for doing things like bold, italics, and yes strikethrough. So to enable the strikethrough, you need to flip the bit that corresponds to this flag. The easiest way to do this is to use a bitwise-or on the current flags and a constant that corresponds to a set of flags with only the strikethrough flag enabled.

Edit from Comment by Ε Г И І И О :

For any one wanting to remove this flag, this is how:

someTextView.setPaintFlags(someTextView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

It is really easy if you are using strings:

<string name="line"> Not crossed <strike> crossed </strike> </string>

And then just:

<TextView 
        ...
         android:text="@string/line"
 />

If you are using Kotlin:

your_text_view.apply {
    paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
    text = "Striked thru text"
}

You can do this in three ways, by either setting foreground in TextView or setting PaintFlag or declaring a string as <strike>your_string</strike> in strings.xml. For example,

Through PaintFlag

This is the simplest method you just have to set strikethrough flag on your TextView as,

yourTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

it will strike through your TextView.

Through foreground drawable(Works only for API 23+)

If your minSdkVersion is API version 23 +, then you can strike through your TextView by setting a foreground as,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="line">
            <stroke android:width="1dp" android:color="@android:color/holo_red_dark"/>
        </shape>
    </item>
</selector>

Now, you just have to set above drawable in your TextView as foreground. For example,

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your Textview with StrikeThrough"
    android:foreground="@drawable/strikethrough_foreground" />  <!-- this is available above --!>

Through strings.xml

In this method, you have to declare your string in strings.xml as strike through as,

<string name="strike_line"> <strike>This line is strike throughed</strike></string>

Note

But I recommend you to strike through your TextView by setting foreground drawable. Because through drawable you can easily set your strike-through line color(as like I set as red color in above example) or size or any other style property. While in the other two methods default text color is strikethrough color.


try this :

richTextView = (TextView)findViewById(R.id.rich_text);  

    // this is the text we'll be operating on  
    SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  

    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  

    // make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox  
    text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);  

    // make "dolor" (characters 12 to 17) display a toast message when touched  
    final Context context = this;  
    ClickableSpan clickableSpan = new ClickableSpan() {  
        @Override  
        public void onClick(View view) {  
            Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();  
        }  
    };  
    text.setSpan(clickableSpan, 12, 17, 0);  

    // make "sit" (characters 18 to 21) struck through  
    text.setSpan(new StrikethroughSpan(), 18, 21, 0);  

    // make "amet" (characters 22 to 26) twice as big, green and a link to this site.  
    // it's important to set the color after the URLSpan or the standard  
    // link color will override it.  
    text.setSpan(new RelativeSizeSpan(2f), 22, 26, 0);  
    text.setSpan(new URLSpan("http://www.djsad.com"), 22, 26, 0);  
    text.setSpan(new ForegroundColorSpan(Color.GREEN), 22, 26, 0);  

    // make our ClickableSpans and URLSpans work  
    richTextView.setMovementMethod(LinkMovementMethod.getInstance());  

    // shove our styled text into the TextView          
    richTextView.setText(text, BufferType.SPANNABLE);