1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/widget/EllipsisTextView.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.widget; 1.10 + 1.11 +import org.mozilla.gecko.R; 1.12 + 1.13 +import android.content.Context; 1.14 +import android.content.res.TypedArray; 1.15 +import android.util.AttributeSet; 1.16 +import android.widget.TextView; 1.17 + 1.18 +/** 1.19 + * Text view that correctly handles maxLines and ellipsizing for Android < 2.3. 1.20 + */ 1.21 +public class EllipsisTextView extends TextView { 1.22 + private final String ellipsis; 1.23 + 1.24 + private int maxLines; 1.25 + private CharSequence originalText; 1.26 + 1.27 + public EllipsisTextView(Context context) { 1.28 + this(context, null); 1.29 + } 1.30 + 1.31 + public EllipsisTextView(Context context, AttributeSet attrs) { 1.32 + this(context, attrs, android.R.attr.textViewStyle); 1.33 + } 1.34 + 1.35 + public EllipsisTextView(Context context, AttributeSet attrs, int defStyle) { 1.36 + super(context, attrs, defStyle); 1.37 + 1.38 + ellipsis = getResources().getString(R.string.ellipsis); 1.39 + 1.40 + TypedArray a = context.getTheme() 1.41 + .obtainStyledAttributes(attrs, R.styleable.EllipsisTextView, 0, 0); 1.42 + maxLines = a.getInteger(R.styleable.EllipsisTextView_ellipsizeAtLine, 1); 1.43 + a.recycle(); 1.44 + } 1.45 + 1.46 + public void setOriginalText(CharSequence text) { 1.47 + originalText = text; 1.48 + setText(text); 1.49 + } 1.50 + 1.51 + @Override 1.52 + public void onLayout(boolean changed, int left, int top, int right, int bottom) { 1.53 + super.onLayout(changed, left, top, right, bottom); 1.54 + 1.55 + // There is extra space, start over with the original text 1.56 + if (getLineCount() < maxLines) { 1.57 + setText(originalText); 1.58 + } 1.59 + 1.60 + // If we are over the max line attribute, ellipsize 1.61 + if (getLineCount() > maxLines) { 1.62 + final int endIndex = getLayout().getLineEnd(maxLines - 1) - 1 - ellipsis.length(); 1.63 + final String text = getText().subSequence(0, endIndex) + ellipsis; 1.64 + // Make sure that we don't change originalText 1.65 + setText(text); 1.66 + } 1.67 + } 1.68 +}