Returns TextView height. - Android User Interface

Android examples for User Interface:TextView

Description

Returns TextView height.

Demo Code


import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils.TruncateAt;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

public class Main{
    protected static final String TAG = DisplayUtils.class.getSimpleName();
    /**// www .  ja v a 2 s. c  o m
     * Returns TextView height.
     * 
     * @param textView
     * @param textViewWidth
     * @return TextView height
     */
    public static int getTextViewHeight(TextView textView, int textViewWidth) {
        int lineHeight = getTextHeight(textView);
        // getTextViewLineCount() will return 0 if string is empty.
        if (textView.getText() == null || textView.getText().equals(""))
            return lineHeight;
        return lineHeight * getTextViewLineCount(textView, textViewWidth);
    }
    /**
     * Returns TextView's text height(text height means line height, not TextView height).
     * <p>TextView default size is 14sp. Note that text size is not equal to text width and height.
     * <p>Note that padding is not in calculation scope.
     * <p>Note that TextView.getHeight() is higher than DisplayUtils.getTextHeight().
     * DisplayUtils.getTextHeight() should be equal to TextView.getLineHeight().
     * 
     * @param tv
     * @return text height in pixel(Even though text is empty, it can calculate height).
     */
    public static int getTextHeight(TextView tv) {
        Paint paint = tv.getPaint();
        FontMetrics fm = paint.getFontMetrics();
        float textHeight = fm.descent - fm.ascent;
        Log.v(TAG, "TextView size is " + tv.getTextSize()
                + " sp, textHeight " + textHeight);
        return (int) (textHeight + 0.5f);
    }
    /**
     * Returns TextView line count.
     * Note that this method will ignore line separator "\n".
     * 
     * @param textView
     * @param textViewWidth
     * @return TextView line count
     */
    public static int getTextViewLineCount(TextView textView,
            int textViewWidth) {
        float textSize = textView.getTextSize();
        Typeface textTypeface = textView.getTypeface(); // may be null.
        TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG
                | Paint.SUBPIXEL_TEXT_FLAG);
        paint.setTextSize(textSize);
        paint.setTypeface(textTypeface);
        int lineCount = 0;
        int index = 0;
        int length = textView.getText().length();
        while (index < length - 1) {
            index += paint.breakText(textView.getText(), index, length,
                    true, textViewWidth, null);
            lineCount++;
        }
        // System.out.println("TextView textSize = " + textSize + ", textTypeface = " + textTypeface + ", lineCount = " + lineCount);
        return lineCount;
    }
}

Related Tutorials