Question

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?

Was it helpful?

Solution

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

In XML: ‑

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

OTHER TIPS

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:

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top