title: Prevent Android's TextView from breaking links
body: <p>This question is probably the same as <a href="https://stackoverflow.com/questions/21094349/prevent-line-break-in-textview">this one</a>, but since none of its answers really solve the problem, I'll ask again.</p>
  
  <p>My app has a TextView that occasionally will display very long URLs. For aesthetic reasons (and since the URLs contain no spaces), the desirable behaviour would be to fill each line completely before jumping to the next one, something like this:</p>
  
  <pre><code>|http://www.domain.com/som|
  |ething/otherthing/foobar/|
  |helloworld               |
  </code></pre>
  
  <p>What happens instead, is the URL being broke  near the bars, as if they were spaces.</p>
  
  <pre><code>|http://www.domain.com/   |
  |something/otherthing/    |
  |foobar/helloworld        |
  </code></pre>
  
  <p>I tried extending the TextView class and adding a modified version of the <strong><em>breakManually</em></strong> method (<a href="https://stackoverflow.com/questions/14373157/dont-wrap-text-in-android-textview-at-period-in-abbreviation">found here</a>) to trick the TextView and do what I need, calling it on <strong><em>onSizeChanged</em></strong> (overridden). It works fine, except for the fact that the TextView is inside a ListView. When this custom TextView is hidden by the scrolling and brought back, its content goes back to the original breaking behaviour, due to the view being re-drawn without <strong><em>onSizeChanged</em></strong> being called.</p>
  
  <p>I was able to work-around this problem by calling <strong><em>breakManually</em></strong> inside <strong><em>onDraw</em></strong>. This presents the expected behaviour at all times, but at a high performance cost: since <strong><em>onDraw</em></strong> is called whenever the ListView is scrolled and the <strong><em>breakManually</em></strong> method isn't exactly "lightweight", the scrolling gets unacceptably laggy, even on a high-end quad-core device.</p>
  
  <p>Next step was to crawl through the <a href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java" rel="nofollow noreferrer">TextView source code</a>, trying to figure where and how it splits the text, and hopefully override it. This was a complete failure. I (a newbie) spent the whole day fruitlessly looking at code I mostly couldn't understand.</p>
  
  <p>And that brings me here. Can someone please point me the right direction about what I should override (assuming that it's possible)? Or maybe there is a simpler way of achieving what I want?</p>
  
  <p>Here's the <strong><em>breakManually</em></strong> method I mentioned. Due to the use of <strong><em>getWidth()</em></strong>, it only works if called after the view is measured.</p>
  
  <pre><code>private CharSequence breakManually (CharSequence text) {
          int width = getWidth() - getPaddingLeft() - getPaddingRight();
          // Can't break with a width of 0.
          if (width == 0) return text;
          Editable editable = new SpannableStringBuilder(text);
          //creates an array with the width of each character
          float[] widths = new float[editable.length()];
          Paint p = getPaint();
          p.getTextWidths(editable.toString(), widths);
          float currentWidth = 0.0f;
          int position = 0;
          int insertCount = 0;
          int initialLength = editable.length();
          while (position &lt; initialLength) {
              currentWidth += widths[position];
              char curChar = editable.charAt(position + insertCount);
              if (curChar == '\n') {
                  currentWidth = 0.0f;
              } else if (currentWidth &gt; width) {
                  editable.insert(position + insertCount , "\n");
                  insertCount++;
                  currentWidth = widths[position];
              }
              position++;
          }
          return editable.toString();
      }
  </code></pre>
  
  <p>To everyone who bothered reading this, thanks for your time.</p>