I wrote this function to trim letters from the end of the text until it meets a specific width requirement.
The 0.38 function sets the proportions of the screen that I want to fill with this text, in this case it was 38%, since I wanted it to cover ~ 40%, including the addition. Worked on cases that I tested with.
Using this code to call the function below
DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); double maxWidth = (double)metrics.widthPixels*0.38 - 1; TextPaint painter = station.getPaint(); String left = item.getLocationName(); left = trimToLength(painter, left, maxWidth); textView.setText(left);
This is a function.
public String trimToLength(TextPaint painter, String initialText, double width) { String message = initialText; String output = initialText; float currentWidth = painter.measureText(output); while (currentWidth > width) { message = message.substring(0, message.length()-1); output = message + "..."; currentWidth = painter.measureText(output); } return output; }
Kurru
source share