51

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • 3
    Does android respect the soft-hyphen character? (U+00AD) – Anon. Dec 15 '10 at 21:16
  • 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 Dec 15 '10 at 21:18
  • @anon .. yeah soft hyphen character might be interesting. I will check that out. – Manfred Moser Dec 15 '10 at 21:23
  • @Emile well .. I dont know enough about Japanese or Korean but it would certainly be weird to break up the word the first place in other languages.. but it would be good to know. – Manfred Moser Dec 15 '10 at 21:24
  • Latex provided a way to suggest how to separate words. I really don't remember the name, but I would search for how to do it in Latex and check if it works on android. – Macarse Dec 16 '10 at 22:28
  • That does not work. Maybe the soft hyphen works. Still have to try that. – Manfred Moser Dec 16 '10 at 22:48
  • @Manfred Moser: any success with the soft hyphen? – Thomas Ahle Sep 03 '11 at 02:01
  • No I did not have any reproducible luck with that. – Manfred Moser Sep 03 '11 at 04:11
  • 3
    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 Oct 21 '14 at 09:07
  • @arnt See my [answer](http://stackoverflow.com/questions/4454911/hyphenation-in-android/35195787#35195787). Android 4.3 seems to support it at least on SAMSUNG devices. – OneWorld Feb 04 '16 at 08:21
  • 5
    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 Dec 30 '16 at 07:54
  • Does ­ from html will work on Android? – aleksandrbel Jul 03 '18 at 09:00

6 Answers6

14

Its a new thing from Android 6 Marshmellow.

Try adding this to your TextView xml

android:hyphenationFrequency="none"
Athul
  • 801
  • 7
  • 16
  • 3
    In Android Q up it's set to "none" per default. Earlier it wasn't the case. – Malachiasz May 08 '19 at 06:58
  • I want to add setHyphenationFrequency(int) –  Jan 29 '20 at 07:37
  • hyphenation is turned "off" by default from Android Q to increase performance, If you need it, you need to turn it on. https://android-developers.googleblog.com/2019/07/whats-new-for-text-in-android-q.html – Ishaan Kumar May 03 '21 at 19:57
7

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.

  • 8
    Successfully used \u200b for this in xml resource file with strings – mach Nov 03 '15 at 07:00
  • 1
    Soft Hyphens are an extremely useful tool and worked in Android targeting API 21 - \u00AD https://en.wikipedia.org/wiki/Soft_hyphen – CrimsonX Jan 26 '18 at 22:52
  • Like any platform the availability of the common "&" shortcuts used in HTML, depend on where they are used in the application. Some widgets run their text through a parser that handles them, some don't. If you use Internationalization string table functions to handle strings (which you absolutely should) you can use UTF. – Robert Wm Ruedisueli Jul 08 '18 at 13:37
7

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.

OneWorld
  • 17,512
  • 21
  • 86
  • 136
  • I did not follow [duden](http://www.duden.de/rechtschreibung/verbunden) to devide the word correctly. ;) – OneWorld Feb 04 '16 at 08:18
  • 1
    Maybe you should use \u00AD (capital letters on 2 last position)... for me it works perfectly! Thanks! – Geryson Nov 17 '17 at 15:11
6

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);
Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73
3

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

user1519979
  • 1,854
  • 15
  • 26
  • 1
    Is it possible to upload the portable library for Xamarin? – Huby Jan 04 '17 at 12:36
  • 1
    just uploaded it. en_gb, en_us, rus and de_ch is added right now. Added in the readme how to add a new language. https://github.com/sma84/NHyphenator-Mono – user1519979 Jan 04 '17 at 14:42
-3

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

iarwain01
  • 424
  • 3
  • 11
  • 1
    While that helps in some cases I really want the whole text to be visible, but just be wrapped/hyphenated in the right place.. – Manfred Moser Dec 15 '10 at 21:25
  • I haven't used ellipsizing yet, but I thought it shows the whole text. No clue about the hyphenating in the wanted place though. – iarwain01 Dec 15 '10 at 21:33