Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Paint; public class Main { final static float COMPLEXITY = 5.12f; private static String justifyOperation(String s, float width, Paint p) { float holder = (float) (COMPLEXITY * Math.random()); while (s.contains(Float.toString(holder))) { holder = (float) (COMPLEXITY * Math.random()); } String holder_string = Float.toString(holder); float lessThan = width; int timeOut = 100; int current = 0; while ((p.measureText(s) < lessThan) && (current < timeOut)) { s = s.replaceFirst(" ([^" + holder_string + "])", " " + holder_string + "$1"); lessThan = (p.measureText(holder_string) + lessThan) - p.measureText(" "); current++; } String cleaned = s.replaceAll(holder_string, " "); return cleaned; } }