Skip to content
Related Articles

Related Articles

Improve Article

Strikethrough Text in Android

  • Last Updated : 30 Apr, 2021

In this article, we are going to implement the strike-through feature on our TextView using this method. This can be useful when we want to edit our content but also want users to see the previous content. Like on the Amazon app we all have seen that the prices are shown with strikethrough when there is a discount on the item and the current price is shown side by side. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Strikethrough Text in Android Sample GIF

Method 1

Create a drawable file with the name strikethrough and will be adding this drawable file in the text file

<?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/black"/>
        </shape>
    </item>
</selector>

Method 2

Create a string like this and will be adding this string as text in TextView

<string name="strike"> Geeks <strike>For Geeks </strike> </string>

Method 3

Input the following line in the MainActivity.java file.



strike.setPaintFlags(strike.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

Implementation of Three Methods

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/striketext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Geeks For Geeks"
        android:textColor="@color/colorPrimary"
        android:textSize="32sp"
        android:textStyle="bold" />
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/strike"
        android:textColor="@color/colorPrimary"
        android:textSize="32sp"
        android:textStyle="bold" />
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="@drawable/strikethrough"
        android:text="Geeks For Geeks"
        android:textColor="@color/colorPrimary"
        android:textSize="32sp"
        android:textStyle="bold" />
  
    <Button
        android:id="@+id/change"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Change"
        android:textColor="@color/black"
        android:textSize="20dp" />
      
</LinearLayout>

Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView strike;
    Button change;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialise the layout
        strike = findViewById(R.id.striketext);
        change = findViewById(R.id.change);
          
        // click on the button
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // if the text is not having strike then set strike else vice versa
                if (!strike.getPaint().isStrikeThruText()) {
                    strike.setPaintFlags(strike.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                } else {
                    strike.setPaintFlags(strike.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
        });
    }
}

Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :