I want to draw a second string adjacent to first string on the screen canvass. The starting point of the second string should be the width of the first string.I used paint.measuretext()
method of Android. But it returns the width which is lesser than the actual width. So my second String overlaps the first one. Can any body explain how to get the accurate width of a string?
testString1 = "44444444444444444444444444444444444444444";
testString2 = "Sample Test String";
canvas.drawText(testString1,0,50, paint);
int textWidth = (int)paint.measureText(testString1);
canvas.drawText(testString2,textWidth,50, paint);
measuretext() is the more precise measurer. So, you have changed text itself or font or font size or something else between measuring and drawing.
Look also here for other methods of measurement. But don't wait for much - measuretext IS a good measurer and the mistake almost surely is on your side.
New Edit: Check the same function for string1+string1 - if the result will be 2x the previous? If yes - that means that you are awaiting the result in one units and you are getting them in the other ones. (measuretext works in scaled pixels). And maybe paint or canvas use different measures for setting x,y and for text measure.
Edit 2: The incorrect scaling can be defined by emulator. Try different resolution/screen sizes there. If it influences the result, you should find the Paint/Canvas parameter by which you can compensate the screen resolution influence.