Surendra Bharathi
Surendra Bharathi

Reputation: 87

add diagonal strike through text

I am developing an math app where i need to strike the text with diagonal line,if i use strike-through-text the line appears horizontal ,i can't use draw-able because I am already using it for text view background i tried even creating a diagonal line in drawable file with borders ,but no luck I can't do it. Is there anyway to achieve this? I am attaching my text view background file

    <?xml version="1.0" encoding="utf-8" ?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:thickness="0dp"
   android:shape="rectangle">
 <stroke android:width="3dp"
  android:color="#4799E8"/>
 <corners android:radius="5dp" />
  <gradient
    android:startColor="#ffffff"
   android:endColor="#FFFFFF"
   android:type="linear"
   android:angle="270"/> 
  </shape>

Upvotes: 2

Views: 2216

Answers (4)

Markymark
Markymark

Reputation: 3009

Here's a slimmed down version of @sunil's answer above. Also, I changed the angle of the strikethrough line to make it look a bit better on TextViews.

class ObliqueStrikeTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0)
    : TextView(context, attrs, defStyle) {

    private var dividerColor: Int = 0
    private var paint: Paint

    init {
        dividerColor = ContextCompat.getColor(context, R.color.redCarnation)
        paint = Paint().apply {
            color = dividerColor
            strokeWidth = resources.getDimension(R.dimen.strikethrough_line_width)
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //reduce angle by 20%
        val startY = height * 0.8f
        val stopY = height - startY
        canvas.drawLine(0.0f, startY, width.toFloat(), stopY, paint)
    }
}

Upvotes: 0

sunil jain
sunil jain

Reputation: 91

In Kotlin

public class ObliqueStrikeTextView : TextView {


private var dividerColor: Int = 0
private lateinit var paint: Paint

constructor(context: Context) : super(context) {
    init(context)
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    init(context)
}

constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
    init(context)
}

private fun init(context: Context) {
    val resources = context.resources
    //replace with your color
    dividerColor = resources.getColor(R.color.black)

    paint = Paint()
    paint.apply {
        color = dividerColor
        //replace with your desired width
        strokeWidth = resources.getDimension(width)
    }

}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    if(::paint.isInitialized){
        canvas.drawLine(0.0f, height.toFloat(), width.toFloat(), 0.0f, paint)
    }
}
}

Upvotes: 0

Aakash
Aakash

Reputation: 5261

I just want to add a slight modification in @Nakul's answer which is that you don't need to define static dimension for the length of strike through, you can get the width of textview itself.

public class ObliqueStrikeTextView extends TextView
{
    private int dividerColor;
    private Paint paint;

    public ObliqueStrikeTextView(Context context)
    {
        super(context);
        init(context);
    }

    public ObliqueStrikeTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public ObliqueStrikeTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context)
    {
        Resources resources = context.getResources();
        //replace with your color
        dividerColor = resources.getColor(R.color.black);

        paint = new Paint();
        paint.setColor(dividerColor);
        //replace with your desired width
          /*Modification*/
        //Instead of providing static width you can pass the width of textview itself like this
        paint.setStrokeWidth(this.getWidth());
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
    }
}

Upvotes: 1

Nakul
Nakul

Reputation: 1329

You will have to create custom TextView which can achieve this.

This answer will help you create it.

public class ObliqueStrikeTextView extends TextView
{
    private int dividerColor;
    private Paint paint;

    public ObliqueStrikeTextView(Context context)
    {
        super(context);
        init(context);
    }

    public ObliqueStrikeTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public ObliqueStrikeTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context)
    {
        Resources resources = context.getResources();
        //replace with your color
        dividerColor = resources.getColor(R.color.black);

        paint = new Paint();
        paint.setColor(dividerColor);
        //replace with your desired width
        paint.setStrokeWidth(resources.getDimension(R.dimen.vertical_divider_width));
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
    }
}

You can use it in a layout file by fully qualifying the view with your package, like this:

<your.package.name.ObliqueStrikeTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234567890"
        android:textSize="20sp"/>

Upvotes: 3

Related Questions