2

Is there a way to create/override the custom word wrap engine for Android TextView?

For example,

My name is Edwin (US:1113)

Should be displayed as

My name is Edwin

(US:1113)

Instead of

My name is Edwin (US:

1113)

Already tried changing all the possible value for the setting below but nothing good.

Android:maxLines
Android:setHorizontallyScrolling
Android:ellipsize
Android:singleLine

In my findings, word wrap in TextView does not include special characters such as :.

Updated 3.09AM 2/7/2018 UTC

As mentioned by @Umair, line breaking with "\n" is not possible because I don't know the length of the text before the colon :. This will line break the sentence too early before it reached the maximum width of the TextView.

edwin
  • 1,152
  • 1
  • 13
  • 27
  • 1
    have you tried using `\n` ? – Umair Jul 02 '18 at 06:56
  • @Umair No because it should be done dynamically depending on the available width. – edwin Jul 02 '18 at 06:59
  • then use textview as match_parent and after that you can get the width of textview and put your text accordingly. Other then that every device will have it's own configuration so it will be alot difficult get the required result. – Umair Jul 02 '18 at 07:02
  • @Umair Yup, this is what I have done. But the problem I am facing is the colon " **:** " inside the text **(US:1113)**. Android TextView will split the text into **(US:** and **1113)** where they supposed to be considered as a single word. – edwin Jul 02 '18 at 07:06
  • one thing can be done, if you are sure the : will come everytime you display a value, then put a check if width is ending and : is there break the line or split the string, ? – Umair Jul 02 '18 at 07:08
  • @Umair Updated the question. Line breaking is not possible. – edwin Jul 02 '18 at 07:16
  • you can do it like I said you can get the width of textview if it's match_parent width and the text is : then break the line otherwise leave it as it is. – Umair Jul 02 '18 at 07:22
  • @Umair This is a good idea. Thanks. – edwin Jul 02 '18 at 07:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174149/discussion-between-edwin-and-umair). – edwin Jul 02 '18 at 09:29

1 Answers1

2

Use 2 TextViews inside a LinearLayout with horizontal orientation and put My name is Edwin in the 1st and (US:1113) in the 2nd. If there is no space for the 2nd TextView then it will fall to the next line.