Hardik Joshi
Hardik Joshi

Reputation: 9507

Android : Strike out Text with bold or thicker line than default STRIKE_THRU_TEXT_FLAG

I am using following for strike text.

 viewHolder.price_red.setPaintFlags(viewHolder.price_red
                    .getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

Its working but I want to increase size of strike through line.

Can anybody help me how to increase size of line ??

Upvotes: 3

Views: 5158

Answers (3)

j2emanue
j2emanue

Reputation: 62559

Here is how to go about it: use a layer-list and place as your textview background. Here i am assuming your textviews background is white (#FFFFFF)

create in the drawables folder a file called strikethru.xml:

 <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#FFFFFF"/> 
        </shape>
    </item>
    <item>
        <shape android:shape="line">
            <stroke android:width="1dp"
                android:color="#8d8d8d"/> <!-- adjust color you want here -->
        </shape>
    </item>
</layer-list>

then in your textview do this:

    <TextView
                                android:id="@+id/tv_toolbar_prod_price"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"

                                android:paddingRight="3dp"
                                android:text="1,290 B"      

android:background="@drawable/strikethru_line"
                                android:textColor="#070707"
                                android:textSize="13sp" />

the padding right of 3dp makes the strike through more evident.

Upvotes: 1

MKJParekh
MKJParekh

Reputation: 34311

This can not be achieved directly as Android doesn't provide any API for the same.

Now, you will have to go Custom way to draw the thicker line.. 1) DrawLine 2)Use Line Image over EditText

I have tried using a EditText and a ImageView under a RelativeLayout and it worked quite nicely for me.

XML Code :

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/etStrike"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text="Strike Thru Text Here"
            android:textColor="@color/blacktext"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/ivStrike"
            android:layout_width="wrap_content"
            android:layout_height="6dp"
            android:layout_centerInParent="true"
            android:background="@color/blacktext" />
    </RelativeLayout>

JAVA Code :

    EditText etStrike = (EditText)findViewById(R.id.etStrike);
    ImageView ivStrike = (ImageView)findViewById(R.id.ivStrike);

    float textSize = etStrike.getTextSize()/2;
    int textLength = etStrike.getText().length();
    int totalLengthApprox = (int) (textLength * textSize)-textLength;
    int height = 6; // YOUR_REQUIRED_HEIGHT

    RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(totalLengthApprox,height);
    param.addRule(RelativeLayout.CENTER_VERTICAL|RelativeLayout.ALIGN_PARENT_LEFT);
    param.setMargins((int)textSize, 0, 0, 0);
    ivStrike.setLayoutParams(param);

I have tested only a few TextSize and Device Resolutions and most important this can work only for singleline EditText for multiline the logic becomes more complex.

Upvotes: 2

cjds
cjds

Reputation: 8426

You can't change the thickness of the strikethrough line.

As you can see from the docs its just a flag. Either on or off.

There are a couple of options though (more hacks than solutions):

  1. Make the text bold or stroke it. This will automatically stroke the strikethrough too which will make it more visible

  2. Manually draw the line with drawLine. (This would be really difficult to do accurately though)

Upvotes: 3

Related Questions