Is there a way to make a given text fill up the entire TextView
width
no matter on the length of the text by stretching the spaces between characters, similar to how word does so.
3 Answers
Your options are pretty limited with TextView.
SUGGESTION: Try WebView (or equivalent), and you might be able to do something like this:
Force single line of text in element to fill width with CSS
div {
text-align: justify;
}
div:after {
content: "";
display: inline-block;
width: 100%;
}

- 11,962
- 4
- 34
- 48
Based on width of your TextView
, calculate new letter spacing then use TextView.setLetterSpacing(float letterSpacing)
.
See https://developer.android.com/reference/android/widget/TextView.html#setLetterSpacing(float) for more information.

- 2,155
- 2
- 16
- 30
-
Is there a way to automatically set spacing to fit the entire width? – Eddie May 21 '20 at 04:05
-
1@Eddie In my best knowledge, there is no automatic method like that! – TaQuangTu May 21 '20 at 04:15
Late answer, but for anyone else wondering how to do this with a textview:
Use this function
The text must also be alligned to the center and the max lines must be set to 1
public static double even_text_spacing(TextView textView){
float even_letter_spacing_px = textView.getWidth()/(textView.getText().toString().length()+2);
float even_letter_spacing_ems = even_letter_spacing_px/textView.getTextSize();
return even_letter_spacing_ems;
}
This function uses the width of the textview and the amount of letters to calculate even letter spacing in ems.
Usage
textView.setLetterSpacing((float) even_text_spacing(customer_product_details_barcode_numbers));
After applying this to your textView, remember to align the text to the center of the view and set the line spacing to 1.
to center and set max lines programatically:
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setMaxLines(1);
to center and set max lines in XML:
android:textAlignment="center"
android:maxLines="1"

- 1
- 2