Home > Software design >  How to get a StrikeThrough on text ? I mean not TextView just a raw text
How to get a StrikeThrough on text ? I mean not TextView just a raw text

Time:06-29

I have this method which works fine for an EditText or a view.

public SpannableString strikeThrough(String txt){

    SpannableString spannableString = new SpannableString(txt);
    StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
    spannableString.setSpan(strikethroughSpan,0, txt.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannableString;

}

 EditText etChecklistItem = checklistView.findViewById(R.id.et_checklist_item);
 etChecklistItem.setText(strikeThrough(etChecklistItem.getText().toString()));

But my problem is after that, the text doesn't have StrikeThrough.

 StringBuilder stringBuilderItem = new StringBuilder();
            for( String list : itemslist) {
                stringBuilderItem.append(list);
                stringBuilderItem.append("\n");
            }

  String text = stringBuilderItem.toString();
  strikeThrough(text);
 dbHelper.insertChecklist(text);

When I get the data in RecyclerView, it would not be in Strikethrough.

CodePudding user response:

Just Checkout this Code

First you need to initialise you edit text or text view

    textView = findViewById(R.id.textView);
    textView.setText("Hello World");
    textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

I hope this will help you, if any problem just comment down

CodePudding user response:

Just add this single line of code in your activity

textView.setText("I want like that")
textView.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

or if you want to strike through text in strings.xml

<string name="yourName"><strike>I want like that</strike></string>
  • Related