2

Trying to build a shopping cart and want to display discounted price and for old price want to show it as in image

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
charan ghumman
  • 459
  • 6
  • 10
  • 3
    See this post I looked up with "android strikethrough" http://stackoverflow.com/questions/3881553/is-there-an-easy-way-to-strike-through-text-in-an-app-widget – Urs Feb 11 '16 at 21:03

5 Answers5

5

DO This:

   TextView someTextView = (TextView) findViewById(R.id.some_text_view); 
   someTextView.setText(someString); // SomeString = your old price
   someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
dhun
  • 633
  • 7
  • 13
2

Dinamically (at runtime) you need to do:

TextView myTextView= (TextView) findViewById(R.id.some_text_view); 
myTextView.setText("myOldPriceHere");
myTextView.setPaintFlags(myTextView.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
1

There are 2 options for this:

Using an TextView and the Paint.STRIKE_THRU_TEXT_FLAG flag

Paint flag that applies a strike-through decoration to drawn text.

This is an example:

TextView someTextView = (TextView) findViewById(R.id.some_text_view);
someTextView.setText("$29,500");
myTextView.setPaintFlags(myTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

enter image description here

Or using an Spannable Text, this is an example:

TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView .setText("$29,500", TextView.BufferType.SPANNABLE);
final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) myTextView.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, myTextView.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannable);

enter image description here

As you can see both options are pretty similar, so you can choose one of them.

Jorgesys
  • 119,885
  • 23
  • 317
  • 256
0
textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  

textView.getPaint().setFlags(0);  //make it gone 
ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
zkywalker
  • 161
  • 1
  • 7
0

You can use Spannable to achieve this, its more flexible in case you need to put this only in a part of the text. Here is an example:

textview.setText("29,500", TextView.BufferType.SPANNABLE);
final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) textview.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, textview.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spannable);
JpCrow
  • 4,433
  • 3
  • 29
  • 46