Java tutorial
//package com.java2s; import android.graphics.Paint; import android.graphics.Rect; public class Main { private static void setTextSizeForWidth(String text, Paint paint, float desiredWidth) { final float testTextSize = 48f; paint.setTextSize(testTextSize); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); float desiredTextSize = testTextSize * desiredWidth / bounds.width(); paint.setTextSize(desiredTextSize); } }