Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"word-wrap: break-word" in EditText

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
like image 335
Raskilas Avatar asked Mar 09 '14 22:03

Raskilas


1 Answers

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());
like image 168
andkorsh Avatar answered Sep 22 '22 00:09

andkorsh