create strikethrough text?

Can I create strikethrough text in Android, I mean adding a special value to the TextView tag, which 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"/> 
+108
android user-interface
Mar 20 '12 at 12:14
source share
9 answers

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); 

To draw text, there are several bit flags for things like bold, italics, and yes strikethrough. Thus, to enable strikethrough, you need to flip the bit corresponding to this flag. The easiest way to do this is to use bitwise or current flags and a constant that matches a set of flags with only the strikeout flag enabled.

Editing from a comment Ε G I І AND O :

For anyone who wants to remove this flag, here's how:

 someTextView.setPaintFlags(someTextView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); 
+280
Mar 20 '12 at 12:20
source share

It is very simple if you use the lines:

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

And then just:

 <TextView ... android:text="@string/line" /> 
+23
Jun 07 '14 at
source share

try the following:

 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 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); 
+19
Mar 20 '12 at 12:18
source share

I just copy my answer . Hope this helps someone. If you have one word, we can use drawable. The following is an 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) 
+14
Dec 05 '14 at 10:02
source share

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

Via PaintFlag

This is the easiest method, you just need to set the crossed out flag in your TextView as,

 yourTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG); 

it will break your TextView.

Across the foreground

You can also break through your TextView by setting the 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 need to set the above drawn 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 --!> 

Via strings.xml

In this method, you should declare your string in strings.xml as through,

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

The note

But I recommend that you get through your TextView by setting the foreground for drawing. Because with drawable you can easily set your hit on the color of the line (as I set as red in the example above) or on the size or any other style property. While in the other two methods, the default text color is to hit the color

+12
Feb 03 '18 at 6:44
source share

If you are using Kotlin:

 your_text_view.apply { paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG text = "Striked thru text" } 
+12
Sep 15 '18 at 11:59
source share

Just use it and you're done. For activities:

 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> 
+7
Mar 26 '18 at 10:02
source share

I tried several options above, but this works best for me:

 String text = "<strike><font color=\'#757575\'>Some text</font></strike>"; textview.setText(Html.fromHtml(text)); 

Hurrah

+3
Jan 16 '18 at 22:34
source share

This fits well with data binding:

 @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}"/> 
+3
Apr 30 '19 at 6:59
source share



All Articles