10

I would like to avoid breaking lines in specifical parts of the String.

Let's say we have this String:

Speed (m/s)

The ideal would be either not jumping at all and having the full String in one line, or, if the jump is needed, in this way:

Speed
(m/s)

What I want to avoid are thing like this:

Speed (m/
s)

Any ideas?

apascual
  • 2,970
  • 1
  • 20
  • 32
  • 1
    http://stackoverflow.com/questions/6134457/how-to-prevent-edittext-from-breaking-a-line-after-punctuation – laalto Aug 28 '13 at 08:02

2 Answers2

11

Try to use non-breaking space between the Speed and "(m/s)". The Unicode no-break space character (\u00A0) should work for TextView.

YX1990
  • 156
  • 5
  • 4
    The problem here is that there is no space in "(m/s)", but Android is splitting it... – apascual Aug 27 '13 at 20:23
  • 1
    Since the "(m/s)" is not a word, android cannot wrap it automatically. This problem looks trickier than the space and word wrap issues.. Here is a question which is similar to yours, maybe you can refer the answer that is accepted. They override the `TextView`with a few modifications. [Similar Question's Link](http://stackoverflow.com/questions/15981941/how-to-control-when-textview-sends-string-to-new-line?rq=1) – YX1990 Aug 27 '13 at 21:43
  • And note that for some languages like Korean, Chinese, etc, there are no spaces in between. So this this only works for languages that use spaces. – Tianyao 'Till' Chen Jan 22 '21 at 13:51
0

Try this (just an idea, not tested):

  1. Calculate the width of the TextView with View.getMeasuredWidth().
  2. Calculate the width of the String with Paint.measureText().
  3. Compare the widths and add "\n" between "Speed" and "(m/s)" if required.
malefiz
  • 39
  • 4
  • I was looking more for a general case... I could get difficult if having several lines and rules at the same time... – apascual Aug 27 '13 at 20:22