FileDocCategorySizeDatePackage
ResizingTextView.javaAPI DocAndroid 5.1 API10374Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

ResizingTextView

public class ResizingTextView extends android.widget.TextView

A {@link android.widget.TextView} that adjusts text size automatically in response to certain trigger conditions, such as text that wraps over multiple lines.

hide

Fields Summary
public static final int
TRIGGER_MAX_LINES
Trigger text resize when text flows into the last line of a multi-line text view.
private int
mTriggerConditions
private int
mResizedTextSize
private boolean
mMaintainLineSpacing
private int
mResizedPaddingAdjustmentTop
private int
mResizedPaddingAdjustmentBottom
private boolean
mIsResized
private boolean
mDefaultsInitialized
private int
mDefaultTextSize
private float
mDefaultLineSpacingExtra
private int
mDefaultPaddingTop
private int
mDefaultPaddingBottom
Constructors Summary
public ResizingTextView(android.content.Context ctx, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)


             
        super(ctx, attrs, defStyleAttr);
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.lbResizingTextView,
                defStyleAttr, defStyleRes);

        try {
            mTriggerConditions = a.getInt(
                    R.styleable.lbResizingTextView_resizeTrigger, TRIGGER_MAX_LINES);
            mResizedTextSize = a.getDimensionPixelSize(
                    R.styleable.lbResizingTextView_resizedTextSize, -1);
            mMaintainLineSpacing = a.getBoolean(
                    R.styleable.lbResizingTextView_maintainLineSpacing, false);
            mResizedPaddingAdjustmentTop = a.getDimensionPixelOffset(
                    R.styleable.lbResizingTextView_resizedPaddingAdjustmentTop, 0);
            mResizedPaddingAdjustmentBottom = a.getDimensionPixelOffset(
                    R.styleable.lbResizingTextView_resizedPaddingAdjustmentBottom, 0);
        } finally {
            a.recycle();
        }
    
public ResizingTextView(android.content.Context ctx, android.util.AttributeSet attrs, int defStyleAttr)

        this(ctx, attrs, defStyleAttr, 0);
    
public ResizingTextView(android.content.Context ctx, android.util.AttributeSet attrs)

        // TODO We should define our own style that inherits from TextViewStyle, to set defaults
        // for new styleables,  We then pass the appropriate R.attr up the constructor chain here.
        this(ctx, attrs, android.R.attr.textViewStyle);
    
public ResizingTextView(android.content.Context ctx)

        this(ctx, null);
    
Methods Summary
public booleangetMaintainLineSpacing()

return
whether or not to maintain line spacing when resizing text. The default is true.

        return mMaintainLineSpacing;
    
public intgetResizedPaddingAdjustmentBottom()

return
desired adjustment to bottom padding for resized text

        return mResizedPaddingAdjustmentBottom;
    
public intgetResizedPaddingAdjustmentTop()

return
desired adjustment to top padding for resized text

        return mResizedPaddingAdjustmentTop;
    
public intgetResizedTextSize()

return
the resized text size

        return mResizedTextSize;
    
public intgetTriggerConditions()

return
the trigger conditions used to determine whether resize occurs

        return mTriggerConditions;
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

        if (!mDefaultsInitialized) {
            mDefaultTextSize = (int) getTextSize();
            mDefaultLineSpacingExtra = getLineSpacingExtra();
            mDefaultPaddingTop = getPaddingTop();
            mDefaultPaddingBottom = getPaddingBottom();
            mDefaultsInitialized = true;
        }

        // Always try first to measure with defaults. Otherwise, we may think we can get away
        // with larger text sizes later when we actually can't.
        setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultTextSize);
        setLineSpacing(mDefaultLineSpacingExtra, getLineSpacingMultiplier());
        setPaddingTopAndBottom(mDefaultPaddingTop, mDefaultPaddingBottom);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        boolean resizeText = false;

        final Layout layout = getLayout();
        if (layout != null) {
            if ((mTriggerConditions & TRIGGER_MAX_LINES) > 0) {
                final int lineCount = layout.getLineCount();
                final int maxLines = getMaxLines();
                if (maxLines > 1) {
                    resizeText = lineCount == maxLines;
                }
            }
        }

        final int currentSizePx = (int) getTextSize();
        boolean remeasure = false;
        if (resizeText) {
            if (mResizedTextSize != -1 && currentSizePx != mResizedTextSize) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, mResizedTextSize);
                remeasure = true;
            }
            // Check for other desired adjustments in addition to the text size
            final float targetLineSpacingExtra = mDefaultLineSpacingExtra +
                    mDefaultTextSize - mResizedTextSize;
            if (mMaintainLineSpacing && getLineSpacingExtra() != targetLineSpacingExtra) {
                setLineSpacing(targetLineSpacingExtra, getLineSpacingMultiplier());
                remeasure = true;
            }
            final int paddingTop = mDefaultPaddingTop + mResizedPaddingAdjustmentTop;
            final int paddingBottom = mDefaultPaddingBottom + mResizedPaddingAdjustmentBottom;
            if (getPaddingTop() != paddingTop || getPaddingBottom() != paddingBottom) {
                setPaddingTopAndBottom(paddingTop, paddingBottom);
                remeasure = true;
            }
        } else {
            // Use default size, line spacing, and padding
            if (mResizedTextSize != -1 && currentSizePx != mDefaultTextSize) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultTextSize);
                remeasure = true;
            }
            if (mMaintainLineSpacing && getLineSpacingExtra() != mDefaultLineSpacingExtra) {
                setLineSpacing(mDefaultLineSpacingExtra, getLineSpacingMultiplier());
                remeasure = true;
            }
            if (getPaddingTop() != mDefaultPaddingTop ||
                    getPaddingBottom() != mDefaultPaddingBottom) {
                setPaddingTopAndBottom(mDefaultPaddingTop, mDefaultPaddingBottom);
                remeasure = true;
            }
        }
        mIsResized = resizeText;
        if (remeasure) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
private voidresizeParamsChanged()

        // If we're not resized, then changing resize parameters doesn't
        // affect layout, so don't bother requesting.
        if (mIsResized) {
            requestLayout();
        }
    
public voidsetMaintainLineSpacing(boolean maintain)
Set whether or not to maintain line spacing when resizing text. The default is true.

param
maintain Whether or not to maintain line spacing

        if (mMaintainLineSpacing != maintain) {
            mMaintainLineSpacing = maintain;
            resizeParamsChanged();
        }
    
private voidsetPaddingTopAndBottom(int paddingTop, int paddingBottom)

        if (isPaddingRelative()) {
            setPaddingRelative(getPaddingStart(), paddingTop, getPaddingEnd(), paddingBottom);
        } else {
            setPadding(getPaddingLeft(), paddingTop, getPaddingRight(), paddingBottom);
        }
    
public voidsetResizedPaddingAdjustmentBottom(int adjustment)
Set the desired adjustment to bottom padding for resized text.

param
adjustment The adjustment to bottom padding, in pixels

        if (mResizedPaddingAdjustmentBottom != adjustment) {
            mResizedPaddingAdjustmentBottom = adjustment;
            resizeParamsChanged();
        }
    
public voidsetResizedPaddingAdjustmentTop(int adjustment)
Set the desired adjustment to top padding for resized text.

param
adjustment The adjustment to top padding, in pixels

        if (mResizedPaddingAdjustmentTop != adjustment) {
            mResizedPaddingAdjustmentTop = adjustment;
            resizeParamsChanged();
        }
    
public voidsetResizedTextSize(int size)
Set the text size for resized text.

param
size The text size for resized text

        if (mResizedTextSize != size) {
            mResizedTextSize = size;
            resizeParamsChanged();
        }
    
public voidsetTriggerConditions(int conditions)
Set the trigger conditions used to determine whether resize occurs. Pass a union of trigger condition constants, such as {@link ResizingTextView#TRIGGER_MAX_LINES}.

param
conditions A union of trigger condition constants

        if (mTriggerConditions != conditions) {
            mTriggerConditions = conditions;
            // Always request a layout when trigger conditions change
            requestLayout();
        }