I want to calculate string font width in pixels in android. I am having string array and I am appending all these array elements to form one string. so I ha
I don't know if this works, but it can.
int textSize=20;
Paint p=new Paint();
p.setTextSize(textSize);
String string="Hello im a string.";
public int getStringWidt()
{
return string.length*textSize;
}
Thanks Jake for the working solution, yet I found this to work for me too:
someText = "Lorem Ipsum";
TextView myTextView = (TextView) findViewById(R.id.myTextView);
float w = myTextView.getPaint().measureText(someText);
Getting the Paint directly from the TextView that I'm going to work with makes me feel that the anwser will be more accurate.
Looks like there is a measureText
method available on Paint. I also found an example:
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setTextSize(64);
mPaint.setTypeface(Typeface.create(Typeface.SERIF, Typeface.ITALIC));
// ...
float w = mPaint.measureText(text, 0, text.length());