Hyphenation in Android

AndroidTextviewWord WrapHyphenation

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

Its a new thing from Android 6 Marshmellow.

Try adding this to your TextView xml

android:hyphenationFrequency="none"

Solution 2 - Android

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.

Solution 3 - Android

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);

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManfred MoserView Question on Stackoverflow
Solution 1 - AndroidAthulView Answer on Stackoverflow
Solution 2 - AndroidRobert Wm RuedisueliView Answer on Stackoverflow
Solution 3 - AndroidMathew KurianView Answer on Stackoverflow
Solution 4 - AndroidOneWorldView Answer on Stackoverflow
Solution 5 - Androiduser1519979View Answer on Stackoverflow
Solution 6 - Androidiarwain01View Answer on Stackoverflow