Skip to content
This repository was archived by the owner on Sep 7, 2020. It is now read-only.

Commit fbccba0

Browse files
author
Dominik Schürmann
committed
Merge pull request #37 from derekcsm/improvement/remove_fromhtml_space
Add setRemoveFromHtmlSpace function/ cleanup example
2 parents 17972e2 + ecf0090 commit fbccba0

File tree

5 files changed

+158
-77
lines changed

5 files changed

+158
-77
lines changed

HtmlTextView/src/main/java/org/sufficientlysecure/htmltextview/HtmlTextView.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class HtmlTextView extends JellyBeanSpanFixTextView {
3131
public static final boolean DEBUG = false;
3232
boolean mDontConsumeNonUrlClicks = true;
3333
boolean mLinkHit;
34+
private boolean removeFromHtmlSpace = false;
3435
private ClickableTableSpan mClickableTableSpan;
3536
private DrawTableLinkSpan mDrawTableLinkSpan;
3637

@@ -46,6 +47,13 @@ public HtmlTextView(Context context) {
4647
super(context);
4748
}
4849

50+
/**
51+
* Note that this must be called before setting text for it to work
52+
*/
53+
public void setRemoveFromHtmlSpace(boolean removeFromHtmlSpace) {
54+
this.removeFromHtmlSpace = removeFromHtmlSpace;
55+
}
56+
4957
public interface ImageGetter {
5058
}
5159

@@ -118,8 +126,11 @@ public void setHtmlFromString(String html, ImageGetter imageGetter) {
118126
final HtmlTagHandler htmlTagHandler = new HtmlTagHandler();
119127
htmlTagHandler.setClickableTableSpan(mClickableTableSpan);
120128
htmlTagHandler.setDrawTableLinkSpan(mDrawTableLinkSpan);
121-
setText(Html.fromHtml(html, htmlImageGetter, htmlTagHandler));
122-
129+
if (removeFromHtmlSpace) {
130+
setText(removeHtmlBottomPadding(Html.fromHtml(html, htmlImageGetter, htmlTagHandler)));
131+
} else {
132+
setText(Html.fromHtml(html, htmlImageGetter, htmlTagHandler));
133+
}
123134
// make links work
124135
setMovementMethod(LocalLinkMovementMethod.getInstance());
125136
}
@@ -153,4 +164,17 @@ public void setClickableTableSpan(ClickableTableSpan clickableTableSpan) {
153164
public void setDrawTableLinkSpan(DrawTableLinkSpan drawTableLinkSpan) {
154165
this.mDrawTableLinkSpan = drawTableLinkSpan;
155166
}
167+
168+
private CharSequence removeHtmlBottomPadding(CharSequence text) {
169+
if (text == null) {
170+
return null;
171+
} else if (text.length() == 0) {
172+
return text;
173+
}
174+
175+
while (text.charAt(text.length() - 1) == '\n') {
176+
text = text.subSequence(0, text.length() - 1);
177+
}
178+
return text;
179+
}
156180
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ See LICENSE for full license text.
119119
- Original [HtmlLocalImageGetter](http://stackoverflow.com/a/22298833) developed by drawk
120120
- [JellyBeanSpanFixTextView](https://gist.github.com/pyricau/3424004) (with fix from comment) developed by Pierre-Yves Ricau
121121
- [Table support](https://github.com/SufficientlySecure/html-textview/pull/33) added by Richard Thai
122+
- [setRemoveFromHtmlSpace](https://github.com/SufficientlySecure/html-textview/pull/37) added by [Derek Smith](https://github.com/derekcsm)
122123

123124
## Contributions
124125

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package org.sufficientlysecure.htmltextview.example;
22

33
import android.app.Activity;
4+
import android.content.Context;
45
import android.content.Intent;
6+
import android.graphics.drawable.Drawable;
57
import android.os.Bundle;
8+
import android.support.v4.content.ContextCompat;
9+
import android.text.Html;
610
import android.view.View;
711

812
import org.sufficientlysecure.htmltextview.ClickableTableSpan;
@@ -33,75 +37,16 @@ public void onClick(View widget) {
3337
protected void onCreate(Bundle savedInstanceState) {
3438
super.onCreate(savedInstanceState);
3539
setContentView(R.layout.activity_main);
40+
3641
HtmlTextView text = (HtmlTextView) findViewById(R.id.html_text);
42+
43+
text.setRemoveFromHtmlSpace(true);
3744
text.setClickableTableSpan(new ClickableTableSpanImpl());
3845
DrawTableLinkSpan drawTableLinkSpan = new DrawTableLinkSpan();
3946
drawTableLinkSpan.setTableLinkText("[tap for table]");
4047
text.setDrawTableLinkSpan(drawTableLinkSpan);
41-
text.setHtmlFromString(
42-
"<h2>Hello world</h2>" +
43-
"<table>" +
44-
" <tr>" +
45-
" <th>Header 1</th>" +
46-
" <th>Header 2</th>" +
47-
" </tr>" +
48-
" <tr>" +
49-
" <td>mo data</td>" +
50-
" <td>mo problems</td>" +
51-
" </tr>" +
52-
"</table>" +
53-
"<br/>" +
54-
"<ul>" +
55-
" <li>cats</li>" +
56-
" <li>dogs Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet Lorem" +
57-
" ipsum dolor sit amet</li>" +
58-
"</ul>" +
59-
"<br/>" +
60-
"<ol>" +
61-
" <li>first</li>" +
62-
" <li>second" +
63-
" <ol>" +
64-
" <li>second - first" +
65-
" <br/>newline" +
66-
" </li>" +
67-
" </ol>" +
68-
" </li>" +
69-
"</ol>" +
70-
"<table>" +
71-
" <tr>" +
72-
" <th>Month</th>" +
73-
" <th>Savings</th>" +
74-
" </tr>" +
75-
" <tr>" +
76-
" <td>January</td>" +
77-
" <td>$100</td>" +
78-
" </tr>" +
79-
" <tr>" +
80-
" <td>" +
81-
" <TABLE>" +
82-
" <TR>" +
83-
" <TH>Header 1</TH>" +
84-
" <TH>Header 2</TH>" +
85-
" </TR>" +
86-
" <TR>" +
87-
" <TD>1st cell</TD>" +
88-
" <TD>2nd cell</TD>" +
89-
" </TR>" +
90-
" </TABLE>" +
91-
" </td>" +
92-
" <td>yo dawg</td>" +
93-
" </tr>" +
94-
"</table>" +
95-
"<br/>" +
96-
"<img src=\"cat\"/>" +
97-
"<br/>A very long text follows below and it contains bold parts. This can" +
98-
"cause a crash " +
99-
"<a href=\"http://code.google.com/p/android/issues/detail?id=35466\">on some" +
100-
" Android versions</a> when using a normal TextView, but our implementation " +
101-
"should workaround that bug." +
102-
"<br/>" +
103-
"<br/>" +
104-
"Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>Lo<b>remips</b>umdol<b>orsita</b>metLorem<b>ipsu</b>mdo<b>lorsit</b>ametLoremip<b>sumdolorsitamet.</b>",
48+
text.setHtmlFromString(getResources().getString(R.string.test_html),
10549
new HtmlTextView.LocalImageGetter());
10650
}
51+
10752
}
Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools"
3-
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
tools:context=".MainActivity">
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="#f8f8f8"
6+
tools:context=".MainActivity">
67

7-
<org.sufficientlysecure.htmltextview.HtmlTextView
8-
android:id="@+id/html_text"
8+
<LinearLayout
99
android:layout_width="match_parent"
1010
android:layout_height="wrap_content"
11-
android:paddingLeft="@dimen/activity_horizontal_margin"
12-
android:paddingRight="@dimen/activity_horizontal_margin"
13-
android:paddingTop="@dimen/activity_vertical_margin"
14-
android:paddingBottom="@dimen/activity_vertical_margin"
15-
android:textAppearance="@android:style/TextAppearance.Small" />
11+
android:orientation="vertical">
12+
13+
<View
14+
android:layout_width="match_parent"
15+
android:layout_height="@dimen/activity_vertical_margin"/>
16+
17+
<org.sufficientlysecure.htmltextview.HtmlTextView
18+
android:id="@+id/html_text"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:layout_marginLeft="@dimen/activity_horizontal_margin"
22+
android:layout_marginRight="@dimen/activity_horizontal_margin"
23+
android:background="@android:color/white"
24+
android:textAppearance="@android:style/TextAppearance.Small"/>
25+
26+
<View
27+
android:layout_width="match_parent"
28+
android:layout_height="@dimen/activity_vertical_margin"/>
29+
30+
</LinearLayout>
1631

1732
</ScrollView>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,99 @@
11
<resources>
22
<string name="app_name">HtmlTextView Example</string>
3+
4+
<string name="test_html"><![CDATA[
5+
<p>
6+
<h2>Hello world</h2>
7+
<table>
8+
<tr>
9+
<th>Header 1</th>
10+
<th>Header 2</th>
11+
</tr>
12+
<tr>
13+
<td>mo data</td>
14+
<td>mo problems</td>
15+
</tr>
16+
</table>
17+
<br/>
18+
<ul>
19+
<li>cats</li>
20+
<li>dogs Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet</li>
21+
</ul>
22+
<br/>
23+
<ol>
24+
<li>first</li>
25+
<li>second
26+
<ol>
27+
<li>second - first
28+
<br/>
29+
newline
30+
</li>
31+
</ol>
32+
</li>
33+
</ol>
34+
<table>
35+
<tr>
36+
<th>Month</th>
37+
<th>Savings</th>
38+
</tr>
39+
<tr>
40+
<td>January</td>
41+
<td>$1,000,000</td>
42+
</tr>
43+
<tr>
44+
<td>
45+
<TABLE>
46+
<TR>
47+
<TH>Header 1</TH>
48+
<TH>Header 2</TH>
49+
</TR>
50+
<TR>
51+
<TH>Cell 1</TH>
52+
<TH>Cell 2</TH>
53+
</TR>
54+
</TABLE>
55+
</td>
56+
<td>yo dawg</td>
57+
</tr>
58+
</table>
59+
<br/>
60+
<img src=\"cat\"/>
61+
<br/>
62+
A very long text follows below and it contains bold parts. This can cause a crash
63+
<a href="http://code.google.com/p/android/issues/detail?id=35466">on some Android versions</a>
64+
when using a normal TextView, but our implementation should workaround that bug.
65+
<br/>
66+
<br/>
67+
<p>
68+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ut eros sed arcu auctor tincidunt
69+
id sit amet elit. Mauris in faucibus neque. Suspendisse facilisis urna nec nisi convallis tincidunt.
70+
Mauris at elit et arcu viverra auctor. Nullam et arcu ultricies, iaculis dolor efficitur, tristique eros.
71+
Interdum et malesuada <b>some bold text in here</b> fames ac ante ipsum primis in faucibus.
72+
Integer nec aliquet mi. Aenean ipsum odio,
73+
iaculis et est eget, condimentum bibendum dui. Aenean nec elementum tortor. Vestibulum tincidunt mi sit
74+
amet tortor semper tincidunt. Sed tincidunt metus et pretium semper. Phasellus neque est, congue nec
75+
metus ac, ullamcorper varius sapien. Integer nec tortor vel justo finibus gravida id quis purus.
76+
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum
77+
aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa,
78+
vel imperdiet augue est non mi.
79+
</p>
80+
<p>
81+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam ut eros sed arcu auctor tincidunt
82+
id sit amet elit. Mauris in faucibus neque. Suspendisse facilisis urna nec nisi convallis tincidunt.
83+
Mauris at elit et arcu viverra auctor. Nullam et arcu ultricies, iaculis dolor efficitur, tristique eros.
84+
Interdum et malesuada fames ac ante ipsum primis in faucibus. Integer nec aliquet mi. Aenean ipsum odio,
85+
iaculis et est eget, condimentum bibendum dui. Aenean nec elementum tortor. Vestibulum tincidunt mi sit
86+
amet tortor semper tincidunt. Sed tincidunt metus et pretium semper. Phasellus neque est, congue nec
87+
metus ac, <b>ullamcorper varius sapien. Integer nec tortor vel justo finibus gravida id quis purus.</b>
88+
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum
89+
aliquam convallis dapibus. Aenean suscipit, orci id elementum vehicula, odio arcu fringilla massa,
90+
vel imperdiet augue est non mi.
91+
</p>
92+
<p>
93+
Android will add extra space at the bottom of the textView by default fromHtml,
94+
use <code>setRemoveFromHtmlSpace(true)</code> on your <b>HtmlTextView</b>
95+
before setting text to prevent this from happening.
96+
</p
97+
</p>
98+
]]></string>
399
</resources>

0 commit comments

Comments
 (0)