Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I define middle-lined text in an Android layout xml file

Tags:

How can I define middle-lined(strikethrough) text in an Android layout xml file?

like image 248
Adham Avatar asked Jun 02 '12 15:06

Adham


People also ask

How do I center align text in XML?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How can I center my text in android?

To align text in TextView at the center vertically and horizontally we need to make use of gravity attribute. Example 1 : You can use center_vertical | center_horizontal value to set the text at the center of the TextView area.

What an XML layout file contains?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View.

How do I center text in RelativeLayout android?

android:gravity controls the appearance within the TextView. So if you had two lines of text they would be centered within the bounds of the TextView. Try android:layout_centerHorizontal="true" . Notice that this won't work if a RelativeLayout contains an attribute android:gravity="center_horizontal".


2 Answers

To strike through, you can use a background image to create the strikethrough effect:

 android:background="@drawable/strike_through" 

Where strike_through drawable is a 9-patch image that keeps a line through the middle. This is the easiest way to implement it.

or you can do it programatically as this.

TextView t = (TextView) findViewById(R.id.text); t.setText("Text here"); t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
like image 62
Anurag Ramdasan Avatar answered Nov 01 '22 13:11

Anurag Ramdasan


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

Android TextView Strikethrough XML

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

  1. Using strike 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");    ` 
like image 32
Emily john Avatar answered Nov 01 '22 11:11

Emily john