6

I have a multiline TextView which can display some optional URL. Now I have a problem: some of my long URLs displayed wrapped in the position of ://

sometext sometext http://   <-- AUTO LINE WRAP
google.com/

How can I disable wrapping for the whole URL or at least for http(s):// prefix? I still need text wrapping to be enabled however.

My text should wrap like that

sometext sometext    <-- AUTO LINE WRAP
http://google.com/
Andrew
  • 3,696
  • 3
  • 40
  • 71

1 Answers1

0

This is just proof of concept to implement custom wrap for textview. You may need to add/edit conditions according to your requirement.

If your requirement is that our textview class must show multiline in such a way that it should not end with certain text ever (here http:// and http:), I have modified the code of very popular textview class over SO to meet this requirement: Source : Auto Scale TextView Text to Fit within Bounds

Changes:

 private boolean mCustomLineWrap = true;

/**
     * Resize the text size with specified width and height
     * @param width
     * @param height
     */
    public void resizeText(int width, int height) {
        CharSequence text = getText();
        // Do not resize if the view does not have dimensions or there is no text
        if(text == null || text.length() == 0 || height <= 0 || width <= 0 || mTextSize == 0) {
            return;
        }

        // Get the text view's paint object
        TextPaint textPaint = getPaint();

        // Store the current text size
        float oldTextSize = textPaint.getTextSize();
        // If there is a max text size set, use the lesser of that and the default text size
        float targetTextSize = mMaxTextSize > 0 ? Math.min(mTextSize, mMaxTextSize) : mTextSize;

        // Get the required text height
        int textHeight = getTextHeight(text, textPaint, width, targetTextSize);

        // Until we either fit within our text view or we had reached our min text size, incrementally try smaller sizes
        while(textHeight > height && targetTextSize > mMinTextSize) {
            targetTextSize = Math.max(targetTextSize - 2, mMinTextSize);
            textHeight = getTextHeight(text, textPaint, width, targetTextSize);
        }



        if(mCustomLineWrap ) {

            // Draw using a static layout
            StaticLayout layout = new StaticLayout(text, textPaint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, false);
            // Check that we have a least one line of rendered text
            if(layout.getLineCount() > 0) {
                String lineText[] = new String[layout.getLineCount()];
                // Since the line at the specific vertical position would be cut off,
                // we must trim up to the previous line
                String wrapStr = "http:", wrapStrWithSlash = "http://";
                boolean preAppendWrapStr = false, preAppendWrapStrWithSlash = false ;
                for(int lastLine = 0; lastLine < layout.getLineCount(); lastLine++)
                {
                    int start = layout.getLineStart(lastLine);
                    int end = layout.getLineEnd(lastLine);
                   lineText[lastLine] = ((String) getText()).substring(start,end);
                    if(preAppendWrapStr)
                    {
                        lineText[lastLine] = "\n" + wrapStr + lineText[lastLine];
                        preAppendWrapStr = false;
                    }
                    else if(preAppendWrapStrWithSlash)
                    {
                        lineText[lastLine] = "\n" + wrapStrWithSlash + lineText[lastLine];
                        preAppendWrapStrWithSlash = false;
                    }

                    if(lineText[lastLine].endsWith(wrapStr))
                    {
                        preAppendWrapStr = true;
                        lineText[lastLine] = lineText[lastLine].substring(0,lineText[lastLine].lastIndexOf(wrapStr));
                    }
                    if( lineText[lastLine].endsWith(wrapStrWithSlash))
                    {
                        preAppendWrapStrWithSlash = true;
                        lineText[lastLine] = lineText[lastLine].substring(0,lineText[lastLine].lastIndexOf(wrapStrWithSlash));
                    }

                }
                String compString = "";
                for(String lineStr :   lineText)
                {
                    compString += lineStr;
                }
                  setText(compString);
            }

        }

        // Some devices try to auto adjust line spacing, so force default line spacing
        // and invalidate the layout as a side effect
        textPaint.setTextSize(targetTextSize);
        setLineSpacing(mSpacingAdd, mSpacingMult);

        // Notify the listener if registered
        if(mTextResizeListener != null) {
            mTextResizeListener.onTextResize(this, oldTextSize, targetTextSize);
        }

        // Reset force resize flag
        mNeedsResize = false;
    }
Community
  • 1
  • 1
Sourab Sharma
  • 2,940
  • 1
  • 25
  • 38
  • Thanks, your solutions works but have you any idea how to keep text spans if any? Now all the text styles inside of textView are cleared because of `setText()` – Andrew Feb 15 '13 at 10:19