Android examples for User Interface:TextView
Returns TextView's text width(not TextView width).
//package com.java2s; import android.graphics.Paint; import android.widget.TextView; public class Main { /**//from w w w. ja va2s .c o m * Returns TextView's text width(not TextView width). * <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. * * @param tv * @return text width in pixel or 0 if TextView.getText() is empty. */ public static int getTextWidth(TextView tv) { /* * It has a little bit deviation(usually smaller than the real value 2px) to calculate TextView width as follow: * Paint paint = new Paint(); // new Paint instance, not TextView's Paint instance. * paint.setTextSize(tv.getTextSize()); * return paint.measureText(tv.getText().toString()); */ Paint paint = tv.getPaint(); // Retrieves TextPaint, subclass of Paint. // Log.v(TAG, "TextView size is " + paint.getTextSize() + " sp."); // If text is empty, it will return 0. return Math.round(paint.measureText(tv.getText().toString())); } }