Can I define text with intermediate text in the XML layout file of Android

How can I define text with an intermediate line (cross out) in an Android layout XML file?

+12
source share
6 answers

To punch, you can use a background image to create a scroll effect:

android:background="@drawable/strike_through" 

In cases where draw_through drawable is a 9-patch image that holds the line through the middle. This is the easiest way to implement it.

or you can do it programmatically like this.

 TextView t = (TextView) findViewById(R.id.text); t.setText("Text here"); t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
+23
source

6 Amazing Ways - Android TextView Strikethrough XML & Kotlin / Java Examples

Android TextView Strikethrough XML

Screenshot - Android TextView Strikethrough XML & Kotlin / Java Example -

  1. Using the hit element.

    strings.xml

<string name="strike_text">1. <strike>StrikeThrough</strike> Using strike</string>

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/strike_text" /> 
  1. Using STRIKE_THRU_TEXT_FLAG

    Kotlin

     textview2.paintFlags = textview2.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG textview2.text = "2. StrikeThrough Using Paint Flags 

    Java

    textview2.setPaintFlags(textview2.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG); textview2.setText("2. StrikeThrough Using Paint Flags");

  2. Using SpannableString

Kotlin

val content1 = "3.1 StrikeThrough Using SpannableString" val spannableString1 = SpannableString(content1) spannableString1.setSpan(StrikethroughSpan(),0,content1.length,0) textview31.text = spannableString1

Java

 textview2.setPaintFlags(textview2.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG); textview2.setText("2. StrikeThrough Using Paint Flags"); ' 
+4
source

To do this only in an XML file, this is what I am doing:

 <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="$36000"/> <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"/> </RelativeLayout> 

Hope this helps!

+2
source
 textview.setPaintFlags(textview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 

You can use the above code in action to strike the text.

And for installation via xml, contact this

+1
source

In Kotlin, this can be done like this:

 your_textView.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG 
+1
source

If you define a BindingAdapter

 @BindingAdapter("strikeThrough") public static void strikeThrough(TextView textView, Boolean strikeThrough) { if (strikeThrough) { textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); } else { textView.setPaintFlags(textView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); } } 

it's simple

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" ... app:strikeThrough="@{true}" .../> 

in the XML layout file.

+1
source

All Articles