Question

I use this to have stike through a textView

textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

but now I wanna personalize the color and rotate it 45° to fit the diagonal of the content.

What I tied:

I though about extending the textview widget and override onDraw method .. but I'm very beginner at designing ...

so any help how to draw the red(color) rotated line on the textView ?

Was it helpful?

Solution

adding to Mayani's response

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

OTHER TIPS

For that, follow the below steps:

  1. Create a custom TextView by extending View class
  2. Declare this custom textview inside XML layout same like we do for TextView, Button widgets.

For example:

  class CustomTextView extends View {
        private int mColor;
        private int mRotationAngle, mRotationW, mRotationH;

            private String mText;
         public CustomTextView(Context context) { 
            super(context);
            // set default parameters
            mColor = Color.WHITE;
            mRotationAngle = 0;
         }

         public void SetColor(int newcolor) {
            mColor = newcolor;
            this.invalidate();
         } 

       public void SetRotation(int newangle, int neww, int newh) {
        mRotationAngle = newangle;
        mRotationW = neww;
        mRotationH = newh;
        this.invalidate();
    }

       public void SetText(String newtext) {
            mText = newtext;
            this.invalidate();
        }

    @Override 
        protected void onDraw(Canvas canvas) { 
            Paint paint = new Paint(); 
            paint.setStyle(Paint.Style.FILL); 
            canvas.rotate(mRotationAngle,mRotationW,mRotationH);
            canvas.drawText(mText, 0, 0, paint);
            super.onDraw(canvas); 
        } 
    }

Update:

Check for Specifying “strikethrough” on a section of TextView text

Using below approach you can achieve this functionality without messing with the UI in your java code:

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true">

                    <TextView
                        android:id="@+id/textview1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:text="$99"
                        android:textSize="12sp" />

                    <View
                        android:layout_width="0dp"
                        android:layout_height="1dp"
                        android:layout_centerVertical="true"
                        android:layout_alignLeft="@+id/textview1"
                        android:layout_alignRight="@+id/textview1"
                        android:background="@color/fav_dark_red_color" />
                </RelativeLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top