Reputation:
I'm using below code on android nougat and it's working:-
Html.fromHtml("<strike> " + myText + "</strike"));
But on Marshemellow it's not working, i mean the <strike>
tag.
Is there any way to get working on all devices ?
myText
is a dynamic text received in recyleView:
public void onBindViewHolder(final DealsAdapter.MyViewHolder holder, final int position) {
final DataDeals feedItem = feedItemList.get(position);
Usage :-
holder.oldPrice.setText(fromHtml("<strike>" + feedItem.getOldPrice() + "</strike>"));
Upvotes: 2
Views: 5631
Reputation: 2665
In addition to what Vyacheslav has mentioned, please check that no Font Family is set on your TextView. I spent nearly 4 hours debugging this issue and removing the FontFamily from the TextView solved my issue.
Upvotes: 0
Reputation: 311
I was having the same problem where StrikethroughSpan was not working (for some devices) on a TextView inside a RecyclerView item layout. It worked fine on my Pixel OS 8.1, but didn't work on a Nexus 6 OS 7.1.1.
In my case I realised that some constraints in my layout were causing the issue. I changed the way I had implemented the layout a little bit and the StrikethroughSpan started working again. Pay attention to TextViews with wrap_content widths and heights and positioned in a Relative layout. Things that might affect the size of the TextView might be causing this issue. Unfortunately I didn't get to the root cause of the problem, but I hope this might help other people with similar problems.
As a note, both solutions to strike through the text work for me:
Html.fromHtml("<strike> " + myText + "</strike"));
or
SpannableStringBuilder spanText = new SpannableStringBuilder("my_awesome_text");
spanText.setSpan(new StrikethroughSpan(), 0, textToBeDisplayed.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spanText);
Upvotes: 2
Reputation: 27221
this method is deprecated.
I should use this code:
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
return result;
}
usage:
fromHtml("<strike> " + myText + "</strike"));
EDIT
Do not forget to close your triangular bracket:
fromHtml("<strike> " + myText + "</strike>"));
instead of your: fromHtml("<strike> " + myText + "</strike"));
Upvotes: 2