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>"));
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"));
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);
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.
Related
What i expected is like this.
This is a movie review,when some words may spoiler story,it will be wrapped by these tags [spoiler][/spoiler].
so i wrote method find out every spoiler words's start index and end index,then using MaskFilterSpan to blur it.
But the result not well.
If i just replace MaskFilterSpan to StrikethroughSpan or BackgroundColorSpan,it works perfect.
My code:
private void testBlurText() {
String text = "Alec Baldwin nailed his job; \n" +
"The animators nailed their job (seriously, the art style and changes were amazing);\n" +
"The ideas guy nailed his job ([spoiler]It's all the imagination of the kid[/spoiler]);\n" +
"The writers just let the story down :( ([spoiler]Although at the beginning it's clear that the whole shebang is the older kid's imagination, the writers probably realised that the script was too short and so the end got changed to something that makes no sense. Why would Tim receive the kid for a second time? - unless it was all a dream...[/spoiler] ).\n" +
"\n" +
"All in all, a good movie, amazing acting and animating, partly let down by a badly written ending to the story. 8/10.";
SpannableString spanText = new SpannableString(text);
List<Pair<Integer, Integer>> spoilersContainer = new ArrayList<>();
getSpoilerIndex(text, 0, spoilersContainer);
for (Pair<Integer, Integer> pair : spoilersContainer) {
spanText.setSpan(new MaskFilterSpan(new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL)), pair.first, pair.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
Logger.d("SpoilerIndex:" + pair.toString());
}
tvTestBlurText.setText(spanText);
}
private void getSpoilerIndex(String rawComment, int beginIndex, List<Pair<Integer, Integer>> spoilersContainer) {
int startIndex = rawComment.indexOf("[spoiler]", beginIndex);
int endIndex = rawComment.indexOf("[/spoiler]", beginIndex);
if (startIndex != -1 && endIndex != -1) {
spoilersContainer.add(new Pair<>(startIndex, endIndex + "[/spoiler]".length()));
getSpoilerIndex(rawComment, endIndex + "[/spoiler]".length(), spoilersContainer);
}
}
Are there any wrong,or just a bug of MaskFilterSpan?
It renders correctly if you set android:hardwareAccelerated="false" on your Activity in AndroidManifest.xml.
BlurMaskFilter is not supported with hardware acceleration.
You can check Unsupported Drawing Operations here:
http://developer.android.com/guide/topics/graphics/hardware-accel.html
I am trying to take a string (which is formatted HTML) and split it at every double enter. I know that before and after a double enter will be a p block tag, and I'm using a for loop to split it in this way. Here is my code
for(String s : rawHTML.split("\\n\\n")){
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
comm.setText(Html.fromHtml(s) );
Expected behavior from this is every "Currently is" in the log should be blank. If there are two or more p blocks, though, when I setText, it overwrites the previous text.
For example, I have a string
<p>Test</p>\n\n<p>Hello</p>
I should be seeing two TextViews like so
[Test]
[Hello]
Instead, I see this
[Hello]
[]
I am really stumped and can't figure out this strange issue.
Thank you!
The following code snippet shoud work for you.
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
for(String s : rawHTML.split("\\n\\n")){
comm.append(Html.fromHtml(s) );
}
I am trying to put a link on a textview, but when I click this link the application breaks!! This is my code:
final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
msg.setText(Html.fromHtml(dbStructure.getMsg()));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());
Where dbStructure.getMsg() returns a String. This String could be something like:
< a href="/reference/android/widget/RelativeLayout.html">RelativeLayout< /a>
lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
It seems nice, but the app stops when I press it.
EDIT
The error thrown ActivityNotFoundException.
the link you are trying to open is broken
/reference/android/widget/RelativeLayout.html
there is nothing corresponding to the above link.
replace it with the proper url like this
http://developer.android.com/reference/android/widget/RelativeLayout.html
Thank you very much for every one... the problem is (as #Antonio #danidee #TheRedFox and #Arslan say) the format of the url... it doesn´t start with http.
With permission from you all, I am going to answer my own question:
final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
String msg_text = dbStructure.getMsg();
if(msg_text.contains("href=\"")) {
String[] msg_aux = msg_text.split("<a href=\"");
if (!msg_aux[1].toLowerCase().startsWith("http"))
msg_aux[1] = "href=\"http://" + msg_aux[1];
else
msg_aux[1] = "href=\"" + msg_aux[1];
msg_text = msg_aux[0] + msg_aux[1];
}
msg.setText(Html.fromHtml(msg_text));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());
Thank you.
EDIT on the code, these lines:
else
msg_aux[1] = "href=\"" + msg_aux[1];
if (Global.DigsSinceLogin > 3) {
int a = Global.DigsSinceLogin;
noCoachingPoints.setText("Excellent Job! \n No Coaching Points Triggered in the Last "+ a +" Digs");}
Requirement:
Need to display value of "a" as 1)bold and 2)yellow color.
Is there a way i can display this value with the above changes and display in the same position
Try Below Code
if (Global.DigsSinceLogin > 3) {
int a = Global.DigsSinceLogin;
noCoachingPoints.setText(Html.fromHtml("Excellent Job!"+"<br>"+" No Coaching Points Triggered in the Last "+ "<font color=yellow><B>" + a + "</B></font>"+" Digs"));}
You can use a SpannableString or SpannableStringBuilder and apply both a StyleSpan and a ForegroundColorSpan to the text that needs special treatment. This much is pretty straightforward; I think the more difficult part will be finding the proper index of the character(s) in the string, especially when you start using localized string resources (which you should).
I have the following:
textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));
The string 'Hello' does turn red but the size does not change.
It is as if the size attribute is just ignored, does anyone know why this is? Am I doing something wrong?
Size attribute seems not working.
You can use <small> or <big> (multiple times to increase the effect)
You can also use <h1> to <h6> (Header only, i.e. add a new line)
Its old-fashion, but it works well !
Yes, size attribute just ignored. Only "color" and "face" attributes are taken into account.
From Html class sources:
private void handleStartTag(String tag, Attributes attributes) {
if (tag.equalsIgnoreCase("br")) {
// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
// so we can safely emite the linebreaks when we handle the close tag.
}
...
else if (tag.equalsIgnoreCase("font")) {
startFont(mSpannableStringBuilder, attributes);
}
...
}
private static void startFont(SpannableStringBuilder text,
Attributes attributes) {
String color = attributes.getValue("", "color");
String face = attributes.getValue("", "face");
int len = text.length();
text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}
Try this one,Its working for me,use small,big key words
TextView mBox = (TextView) findViewById(R.id.txt);
mBox.setText(Html.fromHtml("<font color=#cc0029>" + "<b>"
+ "Hiiiiiiiiii" + "</b>" + "<br />" + "<small>" + "description"
+ "</small>" + "<br />" + "<small>" + "DateAdded" + "</small>"));
Sergey Gotov is right. The only way to change text size it to use h1 - h6 tags.
EDIT: You can also implement TagHandler and use your own tags.
Look at Formatting and Styling on the android developpers site:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Or, on this old post of StackOverflow :
Highlighting Text Color using Html.fromHtml() in Android?