0

I am trying to strikethrough a textview of a listview. I created a string of fruits and another string having values as 1 or 0, if value is1, I want to strikeout that fruit. So as per below code, Apple should be striked out.

Below is my code, I can see system executed line-row.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);, But still Apple entry is not seen as strikes out.

Please help.

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] values = new String[]{"Apple", "Banana", "Orange", "Mango"};
        String[] brk = new String[]{"1", "0", "0", "0"};
        final ListView lv = (ListView) findViewById(R.id.shopList);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);
        System.out.println("value of child count is-" + adapter.getCount());


        for (int t = 0; t < adapter.getCount(); t++) {
            System.out.println("@@@@@@@@@@@@@@@@@Inside loop");
            String itemValue = (String) adapter.getItem(t);
            TextView row = (TextView) adapter.getView(t, null, null);
            System.out.println("item value -" + itemValue);
            System.out.println("corresponding brk -" + brk[t]);
            if (brk[t].equals("1")) {
                row.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                System.out.println("striked -" + brk[t]);
                adapter.notifyDataSetChanged();
            }
        }
        lv.setAdapter(adapter);
    }

Layoutfile-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dhritiapps.dummylv.MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/shopList"></ListView>

</RelativeLayout>
Charuක
  • 12,953
  • 5
  • 50
  • 88
Pankaj Bhardwaj
  • 211
  • 1
  • 2
  • 9
  • post your adapter – Charuක Dec 29 '16 at 10:16
  • Is this similar to what you need ? pls check it out.. http://stackoverflow.com/questions/13658052/set-paint-strike-thru-text-flag-in-textview – Pooja Rajendran C Dec 29 '16 at 10:21
  • Possible duplicate of [Is there an easy way to strike through text in an app widget?](http://stackoverflow.com/questions/3881553/is-there-an-easy-way-to-strike-through-text-in-an-app-widget) – Charuක Dec 29 '16 at 10:23

3 Answers3

1

You can StrikeOut text by Spanneble String:

tv.setText(s, TextView.BufferType.SPANNABLE);
Spannable spannable = (Spannable) tv.getText();
spannable.setSpan(new StrikethroughSpan();, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Gopal
  • 1,734
  • 1
  • 14
  • 34
0
  1. I porpose that you will make a CustomAdapter where you will use the setPainFlags when the view is created and displayed.
  2. if you decide to not create your own adapter then try to use setPainFlags AFTER you attach the adapter to the listView
yotam hadas
  • 702
  • 3
  • 14
0

try this way

row.setPaintFlags(tv.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);

OR

.setPaintFlags( Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

OR You can use Spannablestring

http://androidcocktail.blogspot.in/2014/03/android-spannablestring-example.html

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96