Possible Duplicate:
How to get string width on Android?
I use the following code to calculate the width of the string, but the result of the width is considerably different from the width which displayed in TextView. How can I make this correctly? Thanks in advice.
Paint paint= new Paint();
paint.setTextSize(size);
int iRet = 0;
if (str != null && str.length() > 0) {
int len = str.length();
float[] widths = new float[len];
paint.getTextWidths(str, widths);
for (int j = 0; j < len; j++) {
iRet += (int) Math.ceil(widths[j]);
}
}
return iRet;