Prevent Android Text File Breaks

This question is probably the same as this one , but since none of its answers will solve the problem, I will ask again.

My application has a TextView that sometimes displays very long URLs. For aesthetic reasons (and since URLs do not contain spaces), it would be desirable to completely fill out each line before moving on to the next, something like this:

|http://www.domain.com/som|
|ething/otherthing/foobar/|
|helloworld               |

What happens instead is a URL that breaks near the columns, as if they were spaces.

|http://www.domain.com/   |
|something/otherthing/    |
|foobar/helloworld        |

I tried to extend the TextView class and add a modified version of the breakManally method ( found here ) to fool the TextView and do what I need, calling it onSizeChanged (overridden). It works great, except for the fact that the TextView is inside a ListView. When this custom TextView is hidden when scrolling and returning, its contents return to the original behavior of the violation, because the view is redrawn without calling onSizeChanged .

, breakManally onDraw. , : onDraw , ListView, breakManally "", .

TextView, , , , , . . () , .

. - , ( )? , , , ?

breakManally. - getWidth(), .

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 < initialLength) {
            currentWidth += widths[position];
            char curChar = editable.charAt(position + insertCount);
            if (curChar == '\n') {
                currentWidth = 0.0f;
            } else if (currentWidth > width) {
                editable.insert(position + insertCount , "\n");
                insertCount++;
                currentWidth = widths[position];
            }
            position++;
        }
        return editable.toString();
    }

, , .

+4

Source: https://habr.com/ru/post/1540314/


All Articles