Add StrikeThrough text in android textview programmatically

Set normal text style to StrikeThrough on button click via MainActivity.java programming coding file.

StrikeThrough text means text cut by horizontal straight line at above of it center. It is used to display recently type written or non erasable text and its also used to display recently deleted information on web page or android application. So here is the complete step by step tutorial for Add StrikeThrough text in android textview programmatically.

android-project-download-code-button

How to Add StrikeThrough text in android textview programmatically.

Code for MainActivity.java file.

 package com.textviewstrikethrough_android_examples.com;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

 Button AddSTRIKETHROUGH;
 TextView SampleText;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 AddSTRIKETHROUGH = (Button)findViewById(R.id.button1);
 SampleText = (TextView)findViewById(R.id.textView1);
 
 AddSTRIKETHROUGH.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 SampleText.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.textviewstrikethrough_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="THIS IS SAMPLE TEXT TO APPLY STRIKETHROUGH"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="35dp"
 android:text="ADD STRIKETHROUGH ON ABOVE TEXTVIEW" />

</RelativeLayout>

Screenshot:

before strikethrough

Add StrikeThrough text in android textview programmatically

Click here to download Add StrikeThrough text in android textview programmatically project.

Leave a Reply

Your email address will not be published. Required fields are marked *