michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.util.AttributeSet; michael@0: import android.widget.TextView; michael@0: michael@0: /** michael@0: * Text view that correctly handles maxLines and ellipsizing for Android < 2.3. michael@0: */ michael@0: public class EllipsisTextView extends TextView { michael@0: private final String ellipsis; michael@0: michael@0: private int maxLines; michael@0: private CharSequence originalText; michael@0: michael@0: public EllipsisTextView(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public EllipsisTextView(Context context, AttributeSet attrs) { michael@0: this(context, attrs, android.R.attr.textViewStyle); michael@0: } michael@0: michael@0: public EllipsisTextView(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: michael@0: ellipsis = getResources().getString(R.string.ellipsis); michael@0: michael@0: TypedArray a = context.getTheme() michael@0: .obtainStyledAttributes(attrs, R.styleable.EllipsisTextView, 0, 0); michael@0: maxLines = a.getInteger(R.styleable.EllipsisTextView_ellipsizeAtLine, 1); michael@0: a.recycle(); michael@0: } michael@0: michael@0: public void setOriginalText(CharSequence text) { michael@0: originalText = text; michael@0: setText(text); michael@0: } michael@0: michael@0: @Override michael@0: public void onLayout(boolean changed, int left, int top, int right, int bottom) { michael@0: super.onLayout(changed, left, top, right, bottom); michael@0: michael@0: // There is extra space, start over with the original text michael@0: if (getLineCount() < maxLines) { michael@0: setText(originalText); michael@0: } michael@0: michael@0: // If we are over the max line attribute, ellipsize michael@0: if (getLineCount() > maxLines) { michael@0: final int endIndex = getLayout().getLineEnd(maxLines - 1) - 1 - ellipsis.length(); michael@0: final String text = getText().subSequence(0, endIndex) + ellipsis; michael@0: // Make sure that we don't change originalText michael@0: setText(text); michael@0: } michael@0: } michael@0: }