I have an Android TextView displaying some text, and it's multi-line. However, in the text, I sometimes have domain names; how can I stop the TextView from splitting the lines up on the periods in them?
Is there a unicode non-breaking-period, for example?
To see the issue in action in wrapping an email address, run
android create project --target 16 --path demo --package com.example.demo --activity MainActivity
and change the text in res/layout/main.xml
to "Hello World, MyActivity filler text + email foo@foo.com
". That produces this output on a Galaxy S3 (API level 16):
(Adjust text as appropriate to see wrapping on devices with other screen sizes. Notably, the wrapping is done correctly in Intellij's layout preview, it's only on the device that it's faulty.)
for me didn't work solution of @ozbek respectively @Matt McMinn, I had to change line
for
but otherwise great solution, thanks
Use this :
android:singleLine="true" in xml
TLDR;
@Matt McMinn has already shown a solution for this problem here, go grab it. I am only re-iterating that solution here.
Note that, this issue has already been fixed at platform level in Android 4.2.2. See the below screenshots for word wrap comparison for the same code base but different platform versions on Galaxy Nexus.
Hence, if you are not targeting older versions of Android, you may not wish to use this fix at all.
The code
MainActivity.java:
activity_main.xml:
NonBreakingPeriodTextView.java:
The result
On Android 4.1.2 (Galaxy Nexus)
On Android 2.3.3 (AVD, Nexus One clone)
Hope this helps.
To tell android to parse the domain links in the TextView use this code in the TextView code block:
This will show the domain names as links in the app and wont split the lines.