3

Using this code I am able to display text view with strike to textView :

   holder.discounttext.setText("MRP " + rupee + discountcost);
            holder.discounttext.setPaintFlags(holder.discounttext.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

This is my Current Screen :enter image description here

Desire Screen:

enter image description here

Please tell me how to set strike color with red?

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
Research Development
  • 884
  • 1
  • 19
  • 39

3 Answers3

7

Make a custom TextView

    class CustomTextView extends TextView {
            public Paint paint;
            public boolean addStrike = false;
    public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
        public CustomTextView(Context context) {
            super(context);
            init(context);
        }

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

        private void init(Context context) {
            paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStrokeWidth(getResources().getDisplayMetrics().density * 1);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            if (addStrike) {
                canvas.drawLine(0, getHeight() / 2, getWidth(),
                        getHeight() / 2, paint);
            }
        }

    }

for adding stoke you can call

myCustomTextView.addStrike = true;
myCustomTextView.invalidate();

and for removing strike you just call

myCustomTextView.addStrike = false;
myCustomTextView.invalidate();
Akhil Jayakumar
  • 2,262
  • 14
  • 25
5

The easiest way is to use Drawable. The idea is to create a line drawable, and then set the background of TextView with this drawable. Let the Drawable file be line.xml

<item android:state_pressed="false">
    <shape android:shape="line">
        <stroke android:width="2dp"
            android:color="#ffffff" />
    </shape>
</item>

This is the drawable for the line with red color, and 2dp width. [Alter it according to your requirements]

Then on the TextView, set the line.xml drawable as background.

holder.discounttext.setBackground(getResources().getDrawable(R.drawable.line));

The output will be

enter image description here

capt.swag
  • 10,335
  • 2
  • 41
  • 41
0

A couple of solutions:

Community
  • 1
  • 1
Yangfan
  • 1,866
  • 1
  • 11
  • 13