How do you get the text of a TextView
to be Justified (with text flush on the left- and right- hand sides)?
I found a possible solution here, but it do
I think there are two options:
Use something like Pango that specializes in this via the NDK and render text to an OpenGL or other surface.
Use Paint.measureText() and friends to get the lengths of words and lay them out manually on a Canvas in a custom view.
This line of code will make your text justified
android:justificationMode="inter_word"
You have to set
android:layout_height="wrap_content"
and
android:layout_centerInParent="true"
FILL_HORIZONTAL
is equivalent to CENTER_HORIZONTAL
.
You can see this code snippet in textview's source code:
case Gravity.CENTER_HORIZONTAL:
case Gravity.FILL_HORIZONTAL:
return (mLayout.getLineWidth(0) - ((mRight - mLeft) -
getCompoundPaddingLeft() - getCompoundPaddingRight())) /
getHorizontalFadingEdgeLength();
This doesn't really justify your text but
android:gravity="center_horizontal"
is the best choice you have.
I found a way to solve this problem, but this may not be very grace, but the effect is not bad.
Its principle is to replace the spaces of each line to the fixed-width ImageSpan (the color is transparent).
public static void justify(final TextView textView) {
final AtomicBoolean isJustify = new AtomicBoolean(false);
final String textString = textView.getText().toString();
final TextPaint textPaint = textView.getPaint();
final SpannableStringBuilder builder = new SpannableStringBuilder();
textView.post(new Runnable() {
@Override
public void run() {
if (!isJustify.get()) {
final int lineCount = textView.getLineCount();
final int textViewWidth = textView.getWidth();
for (int i = 0; i < lineCount; i++) {
int lineStart = textView.getLayout().getLineStart(i);
int lineEnd = textView.getLayout().getLineEnd(i);
String lineString = textString.substring(lineStart, lineEnd);
if (i == lineCount - 1) {
builder.append(new SpannableString(lineString));
break;
}
String trimSpaceText = lineString.trim();
String removeSpaceText = lineString.replaceAll(" ", "");
float removeSpaceWidth = textPaint.measureText(removeSpaceText);
float spaceCount = trimSpaceText.length() - removeSpaceText.length();
float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount;
SpannableString spannableString = new SpannableString(lineString);
for (int j = 0; j < trimSpaceText.length(); j++) {
char c = trimSpaceText.charAt(j);
if (c == ' ') {
Drawable drawable = new ColorDrawable(0x00ffffff);
drawable.setBounds(0, 0, (int) eachSpaceWidth, 0);
ImageSpan span = new ImageSpan(drawable);
spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
builder.append(spannableString);
}
textView.setText(builder);
isJustify.set(true);
}
}
});
}
I put the code on GitHub: https://github.com/twiceyuan/TextJustification
Overview: