How to calculate the width of the string
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/3630086/how-to-get-string-width-on-android">How to get string width on Android?</a>  </p>
</blockquote>



<p>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.</p>

<pre><code>Paint paint= new Paint(); 
        paint.setTextSize(size);


        int iRet = 0;  
        if (str != null &amp;&amp; str.length() &gt; 0) {  
                int len = str.length();  
                float[] widths = new float[len];  
                paint.getTextWidths(str, widths);  
                for (int j = 0; j &lt; len; j++) {  
                    iRet += (int) Math.ceil(widths[j]);  
                }  
        }  
        return iRet;
</code></pre>
