public static String ellipsisFilePath(String filePath, BitmapFont font, float maxWidth) { glyphLayout.setText(font, FILE_PATH_ELLIPSIS); float ellipsisWidth = glyphLayout.width; // Cut the last slash int lastSlashIndex = filePath.lastIndexOf("/"); if (lastSlashIndex == filePath.length()-1) { filePath = filePath.substring(0, lastSlashIndex); } // Try to shorten path by cutting slash divided pieces starting from beginning boolean pathCut = false; while (true) { glyphLayout.setText(font, filePath); if (glyphLayout.width < (maxWidth - ellipsisWidth)) break; int slashIndex = filePath.indexOf("/"); if (slashIndex == -1) break; filePath = filePath.substring(slashIndex+1); pathCut = true; } glyphLayout.reset(); // Add ellipsis if path was cut if (pathCut) { filePath = FILE_PATH_ELLIPSIS + filePath; } return filePath; }