9
votes

Has an android a css-like property "word-wrap"?

I just want to my text is not wrapped by spaces, dashes, etc., something like this:

  1. hello, w
  2. orld

Instead of

  1. hello,
  2. world
1

1 Answers

8
votes

Unfortunately, android hasn't this property. But you can replace all breaking characters with ReplacementTransformationMethod.

class WordBreakTransformationMethod extends ReplacementTransformationMethod
{
    private static WordBreakTransformationMethod instance;

    private WordBreakTransformationMethod() {}

    public static WordBreakTransformationMethod getInstance()
    {
        if (instance == null)
        {
            instance = new WordBreakTransformationMethod();
        }

        return instance;
    }

    private static char[] dash = new char[] {'-', '\u2011'};
    private static char[] space = new char[] {' ', '\u00A0'};

    private static char[] original = new char[] {dash[0], space[0]};
    private static char[] replacement = new char[] {dash[1], space[1]};

    @Override
    protected char[] getOriginal()
    {
        return original;
    }

    @Override
    protected char[] getReplacement()
    {
        return replacement;
    }
}

'\u2011' is non-breaking dash, '\u00A0' is non-breaking space. Unfortunately, UTF hasn't non-breaking analog for slash ('/'), but you can use division slash (' ∕ ').

For use this code, set instance of WordBreakTransformationMethod to your EditText.

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());