1

This is quite hard to understand, but I'm trying to find out how to make a TextView adapt to change the text spacing between words on a line which allows the text to reach the very right side of the TextView.

Consider this as an example (this should get my point across):

This one is a line of text which fills the view itself
This   is   another   that   does   the   same   thing

How would I go about making my text react like this? An example application which does this is Pocket, so I know it can be done - I just don't know how.

Any help is appreciated!

whitfin
  • 4,539
  • 6
  • 39
  • 67

1 Answers1

1

What you're referring to is called text justification and is something that has been discussed more than once here on SO in the context of Android.

The short answer is that, unfortunately, justification is currently not (natively) supported by the TextView widget. There are however workarounds that involve either:

  • Manipulating the text in the TextView in such a way that the result is visually close to that of justification. Example.
  • Using a WebView to render the text. Example.

Justifying text on a web page is trivial, but the WebView is a more heavyweight component than a TextView, and hence the feature will come with a performance penalty.

Note that I don't know what approach Pocket is using for their articles, but there are ways to figure that out, and they're not too complicated. That's a completely different can of worms though, so I'll leave it at that.

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Awesome, got it working. Question though; you say WebView is more heavyweight - so would it be possible to use a TextView and use `Html.fromHtml(text);`? Would that make a difference? – whitfin Sep 02 '13 at 18:09
  • I'm pretty sure `Html.fromHtml(text)` will not get the job done because it'll break any justified formatting. [The number of supported html tags is limited](http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html) and any `style` attributes are most likely simply ignored when performing the conversion to a `Spanned`. – MH. Sep 02 '13 at 19:08