HTML.fromHtml line breaks disappearing - android

I am taking Spanned Text from an EditText box and converting it to a HTML tagged string using HTML.toHtml. This works fine. I have verified that the string is correct and contains a <br> in the appropriate location. However, when I got to convert the tagged string back to a spanned text to populate a TextView or EditText using HTML.fromHtml the <br> (or multiple ones if they are present) at the end of the first paragraph disappear. This means that if a users entered text with multiple line breaks and wanted to keep that formatting it gets lost.
I attached a picture to help illustrate this. The first EditText is the user input, the TextView Below it is the HTML.tohtml result of the EditText above it, the EditText below it is populated using HTML.fromHtml using the string in the TextView above it. As you can see the line breaks have disappeared and so have the extra lines. Furthermore, when the spanned text of the second edit text is run through the HTML.toHtml it now produces a different HTML tagged String.
I would like to be able to take the HTML tagged String from the first EditText and populate other TextViews or EditTexts without losing line breaks and formatting.

I also had this problem and I could not find an easy "transform" or something alike solution. Note something important, when the user presses "enter" java produces the special character \n but in HTML there is no such format for line breaking. It is the <br />.
So what I have done was to replace some specific CharSequences, from the plain text, by the alternative HTML format. In my case there was only the "enter" character so it was not that messy.

I had similar problem when I was trying to save/restore editText content to db. The problem is in Html.toHtml, it somehow skips line brakes:
String src = "<p dir=\"ltr\">First line</p><p dir=\"ltr\">Second<br/><br/><br/></p><p dir=\"ltr\">Third</p>";
EditText editText = new EditText(getContext());
// All line brakes are correct after this
editText.setText(new SpannedString(Html.fromHtml(src)));
String result = Html.toHtml(editText.getText()); // Here breaks are lost
// Output :<p dir="ltr">First line</p><p dir="ltr">Second<br></p><p dir="ltr">Third</p>
I've solved this by using custom toHtml function to serialize spanned text, and replaced all '\n' with "< br/>:
public class HtmlParser {
public static String toHtml(Spannable text) {
final SpannableStringBuilder ssBuilder = new SpannableStringBuilder(text);
int start, end;
// Replace Style spans with <b></b> or <i></i>
StyleSpan[] styleSpans = ssBuilder.getSpans(0, text.length(), StyleSpan.class);
for (int i = styleSpans.length - 1; i >= 0; i--) {
StyleSpan span = styleSpans[i];
start = ssBuilder.getSpanStart(span);
end = ssBuilder.getSpanEnd(span);
ssBuilder.removeSpan(span);
if (span.getStyle() == Typeface.BOLD) {
ssBuilder.insert(start, "<b>");
ssBuilder.insert(end + 3, "</b>");
} else if (span.getStyle() == Typeface.ITALIC) {
ssBuilder.insert(start, "<i>");
ssBuilder.insert(end + 3, "</i>");
}
}
// Replace underline spans with <u></u>
UnderlineSpan[] underSpans = ssBuilder.getSpans(0, ssBuilder.length(), UnderlineSpan.class);
for (int i = underSpans.length - 1; i >= 0; i--) {
UnderlineSpan span = underSpans[i];
start = ssBuilder.getSpanStart(span);
end = ssBuilder.getSpanEnd(span);
ssBuilder.removeSpan(span);
ssBuilder.insert(start, "<u>");
ssBuilder.insert(end + 3, "</u>");
}
replace(ssBuilder, '\n', "<br/>");
return ssBuilder.toString();
}
private static void replace(SpannableStringBuilder b, char oldChar, String newStr) {
for (int i = b.length() - 1; i >= 0; i--) {
if (b.charAt(i) == oldChar) {
b.replace(i, i + 1, newStr);
}
}
}
}
Also it turned out that this way is faster in about 4 times that default Html.toHtml(): I've made a benchmark with about 20 pages and 200 spans:
Editable ed = editText.getText(); // Here is a Tao Te Ching :)
String result = "";
DebugHelper.startMeasure("Custom");
for (int i = 0; i < 10; i++) {
result = HtmlParserHelper.toHtml(ed);
}
DebugHelper.stopMeasure("Custom"); // 19 ms
DebugHelper.startMeasure("Def");
for (int i = 0; i < 10; i++) {
result = Html.toHtml(ed);
}
DebugHelper.stopMeasure("Def"); // 85 ms

Replace /n => < br>< br>
example
< p>hi< /p>
< p>j< /p>
to:
< p>hi< /p>< br>< br>< p>j< /p>

Related

How to split up a SpannableStringBuilder while keeping its formating?

I am working on an android project that involves parsing some HTML (parsed by Jsoup) into a SpannableStringBuilder class.
However, I need this SpannableStringBuilder class to be divided up by each new line character into a List once it is done parsing, while keeping its formatting.
Such that a spanned text of
{"I am a spanned text,\n hear me roar"}
would turn into
{
"I am a spanned text,"
"hear me roar"
}
I am fairly new to developing on Android, and could not find anything in the documentation about spitting spans or even getting a listing of all formatting on a spanned class to build my own. So any help is much appreciated.
I managed to figure this out on my own, after looking into the method that pskink suggested.
My solution to this was
#Override
public List<Spanned> parse() {
List<Spanned> spans = new ArrayList<Spanned>();
Spannable unsegmented = (Spannable) Html.fromHtml(base.html(), null, new ReaderTagHandler());
//Set ColorSpan because it defaults to white text color
unsegmented.setSpan(new ForegroundColorSpan(Color.BLACK), 0, unsegmented.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//get locations of '/n'
Stack<Integer> loc = getNewLineLocations(unsegmented);
loc.push(unsegmented.length());
//divides up a span by each new line character position in loc
while (!loc.isEmpty()) {
Integer end = loc.pop();
Integer start = loc.isEmpty() ? 0 : loc.peek();
spans.add(0,(Spanned) unsegmented.subSequence(start, end));
}
return spans;
}
private Stack<Integer> getNewLineLocations(Spanned unsegmented) {
Stack<Integer> loc = new Stack<>();
String string = unsegmented.toString();
int next = string.indexOf('\n');
while (next > 0) {
//avoid chains of newline characters
if (string.charAt(next - 1) != '\n') {
loc.push(next);
next = string.indexOf('\n', loc.peek() + 1);
} else {
next = string.indexOf('\n', next + 1);
}
if (next >= string.length()) next = -1;
}
return loc;
}

Android : Remove last paragraph tag generated by Html.toHtml

I'm editing content that may be html in an EditText. Just before saving the result, I use Html.toHtml to convert the input into a string to be sent to the server. However this method call seems to be generating paragraph tags which I dont need. Eg -
Test edited
seems to get converted to
<p dir="ltr">Test edited</p>
I would like to strip out the last paragraph tag before saving the content. If there are other paragraph tags, I would like to keep those. I have this regex that matches all p tags
"(<p.*>)(.*)(</p>)";
but I'm not sure how to match just the last paragraph and remove just the tags for it.
public static void handleOneParagraph(SpannableStringBuilder text) {
int start = 0;
int end = text.length();
String chars1 = "<p";
if (end < 2)
return;
while (start < end ) {
String seq = text.toString().substring(start, start + 2);
if (seq.equalsIgnoreCase(chars1))
break;
start++;
}
if (text.toString().substring(start, start + 2).equalsIgnoreCase(chars1) ) {
int start2 = start + 1;
String chars2 = ">";
while (start2 < end && !text.subSequence(start2, start2+1).toString().equalsIgnoreCase(chars2) ) {
start2++;
}
if (start2 >= end)
return;
text.replace(start, start2+1, "");
end = text.length();
start = end;
String chars3 = "</p>";
while (start > start2 + 4) {
String last_p = text.subSequence(start - 4, start).toString();
if (last_p.equalsIgnoreCase(chars3) ) {
text.replace(start - 4, start, "");
break;
}
start--;
}
}
}
And now, you can use it like this...
SpannableStringBuilder cleaned_text = new SpannableStringBuilder(Html.toHtml(your_text));
handleOneParagraph(cleaned_text);

Android iritiate through string for specific letter

I have a string, and I can detect if it has an # inside it.
if(title.contains("#")){
SpannableString WordtoSpan = new SpannableString(title);
int idx = title.indexOf("#");
if (idx >= 0) {
int wordEnd = title.indexOf(" ", idx);
if (wordEnd < 0) {
wordEnd = title.length();
}
WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED),
idx,
wordEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
holder.txtTitle.setText(WordtoSpan);
} else {
holder.txtTitle.setText(title);
}
So now, if the string has an # it will color it till the end of the word after it with red. This works perfectly. But the problem is, when a string has more than one # it will color only the first # with it word, not the next or the third etc ..
ex. Now:
Notice it ill color only burger with red
I love chicken #burger , cuz they are #delicious.
I want: Notice bother burger and delicious are colored.
I love chicken #burger , cuz they are #delicious .
use title.split("#") to get a array of strings which contains #.
Something like:
String parts = title.split("#");
for(int i = 0; i<parts.length(); i++){
//TODO: do something with the string part
//parts[i], this is a part of the string.
}
You can use the Pattern to find and match words that contain hashtags in a given string.
String text = "I love chicken #burger, because they are #delicious!";
Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+|\\W+)");
Matcher mat = HASHTAG_PATTERN.matcher(text);
while (mat.find()) {
String tag = mat.group(0);
//String tag will contain the hashtag
//do operations with the hashtag
}

Get the text of the textView selected using Robotium

I am automating a product using robotium. I need to do some dat validation kind of task there.
Scenario is like:
We click on a list, select some items in the list and do some operation.
I want to put the names of the items selected into an array. Such that i can compare it later.
I used the following code:
for(i=0; i<=n;i++)
{
solo.clickInList(i);
Array1[i]=solo.getText(i).toString();
}
But sadly, this statement is not extracting the text of the textView selected but the id of the textView.
Please help me by giving an example of how to get the text of the TextView selected.
At a fix!!
if you have only listview with texts, it should work for you (I didn't test it):
ListView listView = solo.getView(ListView.class, 0);
String text = listView.getItemAtPosition(position));
Another way will be like this:
ArrayList<TextView> result = solo.clickInList(line);
String text = "";
for (int i = 0; i < result.size(); i++) {
text += result.get(i).getText().toString() + " ";
}
if (text.length() > 0) {
text = text.substring(0, text.length() - 1); // remove last space
}

Is there a way to italicize certain words when displaing it in a TextView on Android?

My Android app is displaying text in a TextView.
Are there any tags or anything to put around words that I want italicized? I don't need to set the TextView as italics because the whole sentence would be that way, and I only need specific words italicized.
You need to use a Spannable: see Is there any example about Spanned and Spannable text for an example.
I agree, this isn't a database issue. If this is not a custom app you're working on, you're out of luck. If it is, save the field in the database as HTML.
This function sets a string as the text of a TextView and italicizes one or more spans within that string as long as the span or spans are marked with the tags [i] and [/i]. Of course, it can be modified for other types of styling.
private void doItalicize(TextView xTextView, String xString) {
ArrayList<Integer> IndexStart = new ArrayList<>();
ArrayList<Integer> IndexEnd = new ArrayList<>();
ArrayList<StyleSpan> SpanArray = new ArrayList<>();
int i = 0;
do {
IndexStart.add(i, xString.indexOf("[i]"));
IndexEnd.add(i, xString.indexOf("[/i]") - 3);
xString = xString.replaceFirst("\\[i\\]", "");
xString = xString.replaceFirst("\\[/i\\]", "");
xTextView.setText(xString, TextView.BufferType.SPANNABLE);
SpanArray.add(i, new StyleSpan(Typeface.ITALIC));
Log.d(LOG_TAG, "i: " + i);
i++;
} while (xString.contains("[i]"));
Spannable xSpannable = (Spannable) xTextView.getText();
for (int j = 0; j < i; j++)
xSpannable.setSpan(SpanArray.get(j), IndexStart.get(j), IndexEnd.get(j), Spanned
.SPAN_EXCLUSIVE_EXCLUSIVE);
}
The solution would seem to be to save the content in the database as HTML and then output the HTML in the textview.
See the following article: How to display HTML in TextView?

Categories

Resources