3

I'm trying to display a text like this:

"For more infomration please visit www.my-site.com"

For some resolutions/screens the text is displayed as:

|For more information please visit www.my- |
|site.com                                  |

Can I avoid this effect on the URL part?

Seraphim's
  • 12,559
  • 20
  • 88
  • 129

3 Answers3

1

If this text appears in a standard Activity or Fragment, you can split it into two TextViews, the first containing the info text, the second holding the website URL and setting the attribute android:singleLine="true" to keep the text inside from being wrapped.

Next, you can place these two TextViews side-by-side inside a custom "FlowLayout", which will display them on one line if possible:

|For more information please visit www.my-site.com |  
|                                                  |

or wrap at the TextView boundaries:

|For more information please visit        |  
|www.my-site.com                          |

Unfortuantely, there is no native FlowLayout in Android. You can either write your own, adapting something like a RelativeLayout and writing a custom method to measure the screen width and children Views (for an example of how this is done see How to write Android Autowrap Layout using RelativeLayout and LinearLayout Horizontal with wrapping children).

Or, you can make use of several such layouts that are already available:

Community
  • 1
  • 1
savanto
  • 4,470
  • 23
  • 40
  • I don't want to use android:singleLine="true" or line-break. – Seraphim's May 05 '14 at 15:56
  • @Seraphim'shost What do you mean no line-break? Do you want it all on one line? In that case you can place it inside a ScrollView, and allow the text to disappear off the screen until the user scrolls it. – savanto May 05 '14 at 16:01
  • I don't want to use \n to break the line before the url – Seraphim's May 05 '14 at 16:03
  • 1
    I did not suggest making a line-break, and `android:singleLine` is the attribute to use to prevent TextViews from wrapping the text inside, something they do by default. An alternative solution would be to create a custom View by extending TextView, in which you disable the line-wrapping functionality of the original. – savanto May 05 '14 at 16:09
  • Oh, another possibility: you can change the text size inside the TextView to be larger or smaller depending on the screen size in use, and make sure that you are setting the size to be small enough that the whole line fits. – savanto May 05 '14 at 16:11
  • No way... the size of the resulting text depends on the implementation of each device. – Seraphim's May 05 '14 at 16:12
  • yes, a custom label class could resolve the problem, do you have one? ;) – Seraphim's May 05 '14 at 16:13
1

If your project requirements do not allow you to use a line-break or a custom "FlowLayout" type of layout, you can create a custom View by extending TextView with a custom method to apply a custom line-wrapping scheme.

// String text must contain a portion between
//   <unwrappable></unwrappable> tags.
public void setCustomWrappedText(String text) {
    final String OPEN_TAG = "<unwrappable>";
    final String CLOSE_TAG = "</unwrappable>";

    // Unwrappable string not yet set, find it, between <unwrappable></unwrappable>
    // tags, strip out the tags, and set prefix and suffix.
    int index = text.indexOf(OPEN_TAG);
    int index2 = text.indexOf(CLOSE_TAG);
    String prefix = text.substring(0, index);
    String unwrappable = text.substring(index + OPEN_TAG.length(), index2);
    String suffix = text.substring(index2 + CLOSE_TAG.length());

    // Contents already fit on one line, do nothing.
    this.setText(prefix + unwrappable + suffix);
    int lines = this.getLineCount();
    if (lines < 2)
        return;

    // Set content prefix, ie. text _before_ unwrappable appears, and count lines.
    this.setText(prefix);
    lines = this.getLineCount();

    // Set content to prefix _with_ unwrappable (no suffix), and count lines.
    this.setText(prefix + unwrappable);

    if (this.getLineCount() > lines) // Text has wrapped inside unwrappable, insert a line break to prevent.
        this.setText(prefix + "\n" + this.unwrappable + suffix);
    else // Text may or may not wrap _after_ unwrappable, we don't care.
        this.setText(prefix + this.unwrappable + suffix);
}
savanto
  • 4,470
  • 23
  • 40
  • A solution that uses mark to specify that a sub text is unwrappable would be perfect. Somenthign like this: .setText("this is wrappable text and this is not a warappable text") – Seraphim's May 07 '14 at 10:31
  • @Seraphim'shost Please take a look, I added capability to use text between `` tags. – savanto May 07 '14 at 16:21
  • @Seraphim'shost I see. You only want to set the text programmatically. I made a custom method for you that will do what `onLayout` used to do, take a look. – savanto May 07 '14 at 21:45
  • thanks for your time, the solution to my specific problem was solved in another way. I upvoted your answer because it's interesting. – Seraphim's May 12 '14 at 12:46
1

Use non-breaking hyphen in place of the regular -.

In XML: &#x2011;

In Java: "\u2011"

Of course if you have clickable links, don't replace it in the URL but just the link text.

Example:

<TextView android:layout_width="60dp" android:layout_height="wrap_content"
    android:text="foo foo-foo foo&#x2011;foo"/>

Graphical layout:

enter image description here

laalto
  • 150,114
  • 66
  • 286
  • 303