Reputation: 1805
Can I create a strikethrough text in Android, I mean adding a special value in the TextView
tag that can make this possible?
<TextView
android: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"/>
Upvotes: 169
Views: 139119
Reputation:
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 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));
Upvotes: 358
Reputation: 141
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.
Upvotes: 1
Reputation: 1375
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()
}
}
Upvotes: 4
Reputation: 5734
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()
Upvotes: 5
Reputation: 2296
In Kotlin you can create extension property:
inline var TextView.strike: Boolean
set(visible) {
paintFlags = if (visible) paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
else 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
Upvotes: 17
Reputation: 370
In your observable view model
fun getStrikeContent(): String {
return "Hello"
}
companion object {
@BindingAdapter("strike")
@JvmStatic
fun loadOldPrice(view: TextView, value: String) {
view.text = value
view.paintFlags = STRIKE_THRU_TEXT_FLAG
}
}
then in your xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:strike="@{itemViewModel.strikeContent}" />
Upvotes: 0
Reputation: 3673
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.
Upvotes: 41
Reputation: 19824
This fits nicely into databinding:
@BindingAdapter("strikethrough")
@JvmStatic
fun 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:
<TextView
android: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}"/>
Upvotes: 11
Reputation: 1208
If you are using Kotlin:
your_text_view.apply {
paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
text = "Striked thru text"
}
Upvotes: 46
Reputation: 887
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 :
<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="Rs. 1,999"/>
<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"
android:layout_alignLeft="@id/text_view_original_cash_amount"
android:layout_alignRight="@id/text_view_original_cash_amount" />
</RelativeLayout>
Upvotes: 8
Reputation: 169
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
Upvotes: 3
Reputation: 2438
I am just copying my answer. Hope it will help for someone If 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)
Upvotes: 15
Reputation: 7605
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"
/>
Upvotes: 43
Reputation: 132982
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);
Upvotes: 21