FileDocCategorySizeDatePackage
LabelView.javaAPI DocAndroid 1.5 API6095Wed May 06 22:41:08 BST 2009com.example.android.apis.view

LabelView

public class LabelView extends android.view.View
Example of how to write a custom subclass of View. LabelView is used to draw simple text views. Note that it does not handle styled text or right-to-left writing systems.

Fields Summary
private android.graphics.Paint
mTextPaint
private String
mText
private int
mAscent
Constructors Summary
public LabelView(android.content.Context context)
Constructor. This version is only needed if you will be instantiating the object manually (not from a layout XML file).

param
context

        super(context);
        initLabelView();
    
public LabelView(android.content.Context context, android.util.AttributeSet attrs)
Construct object, initializing with any attributes we understand from a layout file. These attributes are defined in SDK/assets/res/any/classes.xml.

see
android.view.View#View(android.content.Context, android.util.AttributeSet)

        super(context, attrs);
        initLabelView();

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.LabelView);

        CharSequence s = a.getString(R.styleable.LabelView_text);
        if (s != null) {
            setText(s.toString());
        }

        // Retrieve the color(s) to be used for this view and apply them.
        // Note, if you only care about supporting a single color, that you
        // can instead call a.getColor() and pass that to setTextColor().
        setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));

        int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }

        a.recycle();
    
Methods Summary
private final voidinitLabelView()

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(16);
        mTextPaint.setColor(0xFF000000);
        setPadding(3, 3, 3, 3);
    
private intmeasureHeight(int measureSpec)
Determines the height of this view

param
measureSpec A measureSpec packed into an int
return
The height of the view, honoring constraints from measureSpec

        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    
private intmeasureWidth(int measureSpec)
Determines the width of this view

param
measureSpec A measureSpec packed into an int
return
The width of the view, honoring constraints from measureSpec

        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    
protected voidonDraw(android.graphics.Canvas canvas)
Render the text

see
android.view.View#onDraw(android.graphics.Canvas)

        super.onDraw(canvas);
        canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

see
android.view.View#measure(int, int)

        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    
public voidsetText(java.lang.String text)
Sets the text to display in this label

param
text The text to display. This will be drawn as one line.

        mText = text;
        requestLayout();
        invalidate();
    
public voidsetTextColor(int color)
Sets the text color for this label.

param
color ARGB value for the text

        mTextPaint.setColor(color);
        invalidate();
    
public voidsetTextSize(int size)
Sets the text size for this label

param
size Font size

        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();