48
votes

As part of internationalizing an Android application I have come across the need to dynamically word wrap or hyphenate at the right position.

All my strings are externalized in strings.xml files but I have not found any documentation about hyphenation in Android.

I would like to be able to suggest hyphenation positions similar to how I can do it in LaTeX:

http://en.wikipedia.org/wiki/Hyphenation_algorithm

But I have not found any indication if this is possible. Is there anything in the framework I have missed? What are other people doing e.g. with Japanese strings that have no obvious position to break up a sentence? Do you just add spaces at the correct positions?

I could dynamically size the font to fit into certain layouts but for longer messages that go across multiple lines that won't work. What to do?

6
Does android respect the soft-hyphen character? (U+00AD)Anon.
I've had similar problems with Korean and other langauges where single words are huge. So interested in an answer. One question might be is it acceptable to break up the word in the first place?Emile
@anon .. yeah soft hyphen character might be interesting. I will check that out.Manfred Moser
Soft hyphen works in Android 4.4, does not work in 4.2. I would like to know whether it works in 4.3, that's what I hoped to find out on this page.arnt
SDK 17: neither ­­­­\u00AD nor \u200B work. SDK 19: \u200B breaks a word without a hyphen character. SDK 24 - ­­­­\u00AD correct hyphenation, \u200B breaks a word without a hyphen character.Leos Literak

6 Answers

13
votes

Its a new thing from Android 6 Marshmellow.

Try adding this to your TextView xml

android:hyphenationFrequency="none"
7
votes

Management of line breaks can be a hassle. The best option is to use UTF line-break modifier control characters since android supports full UTF

I know someone mentioned "soft-hyphen", but there are quite a few more.

You can also use the "Zero Width Space" between words on languages that lack spaces so you don't have to rely on dictionary interpretation. You can also use this as a soft-hyphen in languages that allow breaking of certain words over lines at certain points.

When using a compound word that you don't want broken, but you want the Text To Speech system to recognize it properly you should use "Word Separator" character. Don't use "Zero Width Non Breaking Space" as that has been deprecated due to it's use as BOM.

Finally, if you want a space but don't want a line break, use a simple non-breaking space.

6
votes

Soft hyphen worked on a Samsung Galaxy device starting with Android 4.3.

<!-- key combination to enter soft hyphen: [Alt Gr]+[-] or [Alt]+240 on German PC, see https://de.wikipedia.org/wiki/Weiches_Trennzeichen#Darstellung_auf_Computersystemen -->
<string name="no_connection">Nicht ver-bund-en</string>

enter image description here

Since my use case was pretty narrow, I just used one soft hyphen in the word "verbunden". The Unicode \u00ad had no effect.

5
votes

The following library supports hyphenation. It does all types of text alignment (left/right/center/justified) and hyphenation for you. Not all languages have been added but can be added as necessary. This library uses NO WEBVIEWS and SUPPORTS SPANNABLES and allows for LONG TEXT.

LIBRARY: https://github.com/bluejamesbond/TextJustify-Android

ANDROID: 2.2 to 5.X

SETUP

DocumentView documentView = addDocumentView(new StringBuilder("Your long text content"), DocumentView.PLAIN_TEXT);
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.getDocumentLayoutParams().setHyphenator(new Hyphenator(HyphenPattern.PT));
documentView.getDocumentLayoutParams().setHyphenated(true);
3
votes

The question is old but just found best solution for me:

I have to say i'm programming in Xamarin, so the code is in C# but should not be a problem to port into java.

I used the NHyphenator Logic (https://github.com/alkozko/NHyphenator) for Inserting SoftHyphens(UTF8 Symbol - 0x00AD) so the Textview do hyphenation at the right place.

To get Hyphenation for other languages i used the openoffice Dictionaries. e.g. i had to get Hyphenation for german-swiss language

http://extensions.openoffice.org/en/search?f[0]=field_project_tags%3A157 1. Download the extension 2. Unzip it with winrar or something else 3. Copy the hyph_xx_xx/hyph_xx_xx.dic file 4. Add new Language to the Hyphenator class

NHyphernator resource files are declared as: hyph-xx-xx.pat.txt --> content of the dic file without comments hyph-xx-xx.hyp.txt --> file which contains word-exceptions where the logic for hyphening does not give correct results

If anyone wants the portable Library for Xamarin just tell, i can upload it.

EDIT:

The breakstrategy should be set to Balanced in TExtview. API Level > 23, else don't use breakstrategy.

EDIT:

Here's the mono/xamarin code: https://github.com/sma84/NHyphenator-Mono

-2
votes

setEllipsize might be of help, if this error is yet fixed.