Can I create a strikethrough text in Android, I mean adding a special value in the TextView
tag that can make this possible?
<TextViewandroid:id="@+id/title" android:layout_width="wrap_content"android:layout_height="wrap_content" android:textColor="#040404"android:typeface="sans" android:textSize="12dip"android:textStyle="bold"/>
Best Answer
You can use the following code copied from another Stack Overflow answer:
TextView someTextView = (TextView) findViewById(R.id.some_text_view);someTextView.setText(someString);someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
See also Paint.STRIKE_THRU_TEXT_FLAG:
For painting text, there are several bit flags for doing things likebold, italics, and yes strikethrough. So to enable the strikethrough,you need to flip the bit that corresponds to this flag. The easiestway to do this is to use a bitwise-or on the current flags and aconstant that corresponds to a set of flags with only thestrikethrough 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));
If you are using Kotlin:
your_text_view.apply {paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAGtext = "Striked thru text"}
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"/>
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,
<TextViewandroid: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);
I am just copying my answer. Hope it will help for someoneIf you have a single word we can use drawable.Following is the example:
<item android:state_pressed="false"><shape android:shape="line"><stroke android:width="2dp" android:color="#ffffff" /></shape></item>
if you have multiple lines you can use the following code:
TextView someTextView = (TextView) findViewById(R.id.some_text_view);someTextView.setText(someString);someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG)
In Kotlin you can create extension property:
inline var TextView.strike: Booleanset(visible) {paintFlags = if (visible) paintFlags or Paint.STRIKE_THRU_TEXT_FLAGelse paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()}get() = paintFlags and Paint.STRIKE_THRU_TEXT_FLAG == Paint.STRIKE_THRU_TEXT_FLAG
And use:
textView.strike = true
This fits nicely into databinding:
@BindingAdapter("strikethrough")@JvmStaticfun strikethrough(view: TextView, show: Boolean) {view.paintFlags = if (show) {view.paintFlags or STRIKE_THRU_TEXT_FLAG} else {view.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()}}
Then in your xml:
<TextViewandroid:id="@+id/line_item_name"android:textAppearance="?attr/textAppearanceBody2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Club sandwich with ranch dressing"app:strikethrough="@{viewModel.isItemChecked}"/>
Just use this and you are done .For Activity :
TextView t= (TextView).findViewById(R.id.thousand));t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
For Xml :
<RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid: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="Rs. 1,999"/><Viewandroid: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"android:layout_alignLeft="@id/text_view_original_cash_amount"android:layout_alignRight="@id/text_view_original_cash_amount" /> </RelativeLayout>
In kotlin:
to do:
textView.paintFlags= textView.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
to undo:
textView.paintFlags= textView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
As suggested by alejandro-h-cruz
, this is the Kotlin extension function to help doing so.
private fun TextView.strikeThrough(shouldStrike: Boolean) {paintFlags = if (shouldStrike) {paintFlags or Paint.STRIKE_THRU_TEXT_FLAG} else {paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()}}
I tried few options above but, this works best for me:
String text = "<strike><font color=\'#757575\'>Some text</font></strike>";textview.setText(Html.fromHtml(text));
cheers
There's another way to change the STRIKE_THRU_TEXT_FLAG bit.
It's by getting the Paint
object associated to the TextView
and using the setStrikeThruText
function like this:
textView.getPaint().setStrikeThruText(/* boolean expression */);
Documentation says about it:
Helper for setFlags(), setting or clearing the STRIKE_THRU_TEXT_FLAG bit
There are functions for setting, clearing or reading other bits too.
In your observable view model
fun getStrikeContent(): String {return "Hello"}companion object {@BindingAdapter("strike")@JvmStaticfun loadOldPrice(view: TextView, value: String) {view.text = valueview.paintFlags = STRIKE_THRU_TEXT_FLAG}}
then in your xml
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:strike="@{itemViewModel.strikeContent}" />