ASKAR ALI
ASKAR ALI

Reputation: 663

How to strike out a textview in android with specifc color

I have been trying to change the textview strike out line colour as RED but it gives me the same colur of the textview. the code which I had tried is

Paint paint=new Paint();
paint.setColor(Color.RED);
textview.setPaintFlags(holder.product_cost.getPaintFlags() | paint.STRIKE_THRU_TEXT_FLAG);

Please help me to fix this.

Iam getting this

Upvotes: 3

Views: 2431

Answers (2)

Anoop M Maddasseri
Anoop M Maddasseri

Reputation: 10549

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);

Upvotes: 1

curiousMind
curiousMind

Reputation: 2812

you can use onDraw method

@Override
   protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(strikeThroughColor);
    paint.setStyle(Paint.Style.FILL); 
    paint.setStrikeThruText(true);
    paint.setStrokeWidth(strikeThroughWidth);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    super.onDraw(canvas);
    float width = getWidth();
    float heigh = getHeight();
    canvas.drawLine(width/10, heigh/10, (width-width/10),(heigh-heigh/10), paint);
} 

refer of this link : How to change color of strikethrough

Upvotes: 0

Related Questions