|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.widget; |
|
7 |
|
8 import org.mozilla.gecko.R; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.content.res.TypedArray; |
|
12 import android.util.AttributeSet; |
|
13 import android.widget.TextView; |
|
14 |
|
15 /** |
|
16 * Text view that correctly handles maxLines and ellipsizing for Android < 2.3. |
|
17 */ |
|
18 public class EllipsisTextView extends TextView { |
|
19 private final String ellipsis; |
|
20 |
|
21 private int maxLines; |
|
22 private CharSequence originalText; |
|
23 |
|
24 public EllipsisTextView(Context context) { |
|
25 this(context, null); |
|
26 } |
|
27 |
|
28 public EllipsisTextView(Context context, AttributeSet attrs) { |
|
29 this(context, attrs, android.R.attr.textViewStyle); |
|
30 } |
|
31 |
|
32 public EllipsisTextView(Context context, AttributeSet attrs, int defStyle) { |
|
33 super(context, attrs, defStyle); |
|
34 |
|
35 ellipsis = getResources().getString(R.string.ellipsis); |
|
36 |
|
37 TypedArray a = context.getTheme() |
|
38 .obtainStyledAttributes(attrs, R.styleable.EllipsisTextView, 0, 0); |
|
39 maxLines = a.getInteger(R.styleable.EllipsisTextView_ellipsizeAtLine, 1); |
|
40 a.recycle(); |
|
41 } |
|
42 |
|
43 public void setOriginalText(CharSequence text) { |
|
44 originalText = text; |
|
45 setText(text); |
|
46 } |
|
47 |
|
48 @Override |
|
49 public void onLayout(boolean changed, int left, int top, int right, int bottom) { |
|
50 super.onLayout(changed, left, top, right, bottom); |
|
51 |
|
52 // There is extra space, start over with the original text |
|
53 if (getLineCount() < maxLines) { |
|
54 setText(originalText); |
|
55 } |
|
56 |
|
57 // If we are over the max line attribute, ellipsize |
|
58 if (getLineCount() > maxLines) { |
|
59 final int endIndex = getLayout().getLineEnd(maxLines - 1) - 1 - ellipsis.length(); |
|
60 final String text = getText().subSequence(0, endIndex) + ellipsis; |
|
61 // Make sure that we don't change originalText |
|
62 setText(text); |
|
63 } |
|
64 } |
|
65 } |