Draw line throughout the TextView
using Ondraw
method.
public class CustomTextView extends TextView {
private int mColor;
private Paint paint;
public CustomTextView (Context context) {
super(context);
init(context);
}
public CustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
Resources resources = context.getResources();
//Color
mColor = resources.getColor(R.color.blue);
paint = new Paint();
paint.setColor(mColor);
//Width
paint.setStrokeWidth(5);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(0, 25, getWidth(), 25, paint);
}
}
Usage
<package.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Strike Me"
android:textSize="50sp"/>
You can customize the positioning of strike and you can do as follows if you need to strike with the color of TextView that you applied.
Using resource file
<resource>
<string id="@+id/strike_one"><strike>Strike Me!</strike></string>
</resources>
Programmatically
TextView text= (TextView) findViewById(R.id.some_label);
text.setText("Strike Me!");
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);