The justification used here is not an android method. It uses line length measurement and space-width responsiveness. I also noticed that sometimes something is not doing well. Below the code, maybe someone will notice something that can be improved.
@SimpleFunction (description = "Returns whether the text is justified by stretching a space.")
public void SetJustify(final Is justify, final Label label) {
//toView(label).setBreakStrategy(LineBreaker.BREAK_STRATEGY_SIMPLE);
final String textString = toView(label).getText().toString();
int index = textString.indexOf(" ");
final float spaceWidth = toView(label).getLayout().getPrimaryHorizontal(index + 1) - toView(label).getLayout().getPrimaryHorizontal(index);
SpannableString str = null;
if (toView(label).getText() instanceof Spannable) {
str = (SpannableString) toView(label).getText();
} else {
str = new SpannableString(toView(label).getText());
}
final SpannableString builder = str;
toView(label).post(new Runnable() {
@Override
public void run() {
if (justify.toBoolean()) {
if (!saveJustify.containsKey(label)) {
final TextPaint textPaint = toView(label).getPaint();
final int lineCount = toView(label).getLineCount();
final int textViewWidth = toView(label).getWidth();
for (int i = 0; i < lineCount; i++) {
int lineStart = toView(label).getLayout().getLineStart(i);
int lineEnd = toView(label).getLayout().getLineEnd(i);
String lineString = textString.substring(lineStart, lineEnd);
String trimSpaceText = lineString.trim();
String removeSpaceText = lineString.replaceAll(" ", "");
float removeSpaceWidth = textPaint.measureText(removeSpaceText) + spaceWidth;
float spaceCount = trimSpaceText.length() - removeSpaceText.length();
float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount;
float value = eachSpaceWidth/spaceWidth;
if (i == lineCount - 1) {
break;
} else if (!lineString.contains("\n")) {
for (int j = lineStart; j < lineEnd; j++) {
char c = textString.charAt(j);
if (c == ' ') {
builder.setSpan(new ScaleXSpan(value), j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
saveJustify.put(label, true);
toView(label).setText(builder);
}
} else {
if (saveJustify.containsKey(label)){
ScaleXSpan[] spans = builder.getSpans(0, textString.length(), ScaleXSpan.class);
if (spans.length > 0){
for (int j = 0; j < spans.length; j++) {
builder.removeSpan(spans[j]);
}
saveJustify.remove(label);
toView(label).setText(builder);
}
}
}
}
});
}






