Skip to content

Commit d1d6435

Browse files
committed
1 parent f0c2034 commit d1d6435

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

res/values/strings.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
</resources>
4-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (C) 2011 Micah Hainline
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.triposo.barone;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import android.content.Context;
23+
import android.graphics.Canvas;
24+
import android.text.Layout;
25+
import android.text.Layout.Alignment;
26+
import android.text.StaticLayout;
27+
import android.text.TextUtils.TruncateAt;
28+
import android.util.AttributeSet;
29+
import android.widget.TextView;
30+
31+
public class EllipsizingTextView extends TextView {
32+
private static final String ELLIPSIS = "...";
33+
34+
public interface EllipsizeListener {
35+
void ellipsizeStateChanged(boolean ellipsized);
36+
}
37+
38+
private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>();
39+
private boolean isEllipsized;
40+
private boolean isStale;
41+
private boolean programmaticChange;
42+
private String fullText;
43+
private int maxLines = -1;
44+
private float lineSpacingMultiplier = 1.0f;
45+
private float lineAdditionalVerticalPadding = 0.0f;
46+
47+
public EllipsizingTextView(Context context) {
48+
super(context);
49+
}
50+
51+
public EllipsizingTextView(Context context, AttributeSet attrs) {
52+
super(context, attrs);
53+
}
54+
55+
public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
56+
super(context, attrs, defStyle);
57+
}
58+
59+
public void addEllipsizeListener(EllipsizeListener listener) {
60+
if (listener == null) {
61+
throw new NullPointerException();
62+
}
63+
ellipsizeListeners.add(listener);
64+
}
65+
66+
public void removeEllipsizeListener(EllipsizeListener listener) {
67+
ellipsizeListeners.remove(listener);
68+
}
69+
70+
public boolean isEllipsized() {
71+
return isEllipsized;
72+
}
73+
74+
@Override
75+
public void setMaxLines(int maxLines) {
76+
super.setMaxLines(maxLines);
77+
this.maxLines = maxLines;
78+
isStale = true;
79+
}
80+
81+
public int getMaxLines() {
82+
return maxLines;
83+
}
84+
85+
@Override
86+
public void setLineSpacing(float add, float mult) {
87+
this.lineAdditionalVerticalPadding = add;
88+
this.lineSpacingMultiplier = mult;
89+
super.setLineSpacing(add, mult);
90+
}
91+
92+
@Override
93+
protected void onTextChanged(CharSequence text, int start, int before, int after) {
94+
super.onTextChanged(text, start, before, after);
95+
if (!programmaticChange) {
96+
fullText = text.toString();
97+
isStale = true;
98+
}
99+
}
100+
101+
@Override
102+
protected void onDraw(Canvas canvas) {
103+
if (isStale) {
104+
super.setEllipsize(null);
105+
resetText();
106+
}
107+
super.onDraw(canvas);
108+
}
109+
110+
private void resetText() {
111+
int maxLines = getMaxLines();
112+
String workingText = fullText;
113+
boolean ellipsized = false;
114+
if (maxLines != -1) {
115+
Layout layout = createWorkingLayout(workingText);
116+
if (layout.getLineCount() > maxLines) {
117+
workingText = fullText.substring(0, layout.getLineEnd(maxLines - 1)).trim();
118+
while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > maxLines) {
119+
int lastSpace = workingText.lastIndexOf(' ');
120+
if (lastSpace == -1) {
121+
break;
122+
}
123+
workingText = workingText.substring(0, lastSpace);
124+
}
125+
workingText = workingText + ELLIPSIS;
126+
ellipsized = true;
127+
}
128+
}
129+
if (!workingText.equals(getText())) {
130+
programmaticChange = true;
131+
try {
132+
setText(workingText);
133+
} finally {
134+
programmaticChange = false;
135+
}
136+
}
137+
isStale = false;
138+
if (ellipsized != isEllipsized) {
139+
isEllipsized = ellipsized;
140+
for (EllipsizeListener listener : ellipsizeListeners) {
141+
listener.ellipsizeStateChanged(ellipsized);
142+
}
143+
}
144+
}
145+
146+
private Layout createWorkingLayout(String workingText) {
147+
return new StaticLayout(workingText, getPaint(), getWidth() - getPaddingLeft() - getPaddingRight(),
148+
Alignment.ALIGN_NORMAL, lineSpacingMultiplier, lineAdditionalVerticalPadding, false);
149+
}
150+
151+
@Override
152+
public void setEllipsize(TruncateAt where) {
153+
// Ellipsize settings are not respected
154+
}
155+
}

0 commit comments

Comments
 (0)