Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:07:04+00:00 2026-06-17T20:07:04+00:00

Is there a way to control where a TextView chooses to wrap its text?

  • 0

Is there a way to control where a TextView chooses to wrap its text? The issue that I’m having is that it wants to wrap at periods – so a string like “Available in the U.S. and Canada” could wrap after the period between the U and the S. Is there a way to tell the TextView not to wrap unless there’s a period and a space, or some programmatic way to control the wrapping?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:07:05+00:00Added an answer on June 17, 2026 at 8:07 pm

    I ended up writing my own algorithm to break the text only on whitespace. I had originally used the breakText method of Paint, but was having some issues (that may actually be resolved in this version of the code, but that’s OK). This isn’t my best chunk of code, and it could definitely be cleaned up a bit, but it works. Since I’m overriding TextView, I just call this from the onSizeChanged method to ensure that there’s a valid width.

    private static void breakManually(TextView tv, Editable editable)
    {
        int width = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight();
        if(width == 0)
        {
            // Can't break with a width of 0.
            return false;
        }
    
        Paint p = tv.getPaint();
        float[] widths = new float[editable.length()];
        p.getTextWidths(editable.toString(), widths);
        float curWidth = 0.0f;
        int lastWSPos = -1;
        int strPos = 0;
        final char newLine = '\n';
        final String newLineStr = "\n";
        boolean reset = false;
        int insertCount = 0;
    
        //Traverse the string from the start position, adding each character's
        //width to the total until:
        //* A whitespace character is found.  In this case, mark the whitespace
        //position.  If the width goes over the max, this is where the newline
        //will be inserted.
        //* A newline character is found.  This resets the curWidth counter.
        //* curWidth > width.  Replace the whitespace with a newline and reset 
        //the counter.
    
        while(strPos < editable.length())
        {
            curWidth += widths[strPos];
    
            char curChar = editable.charAt(strPos);
    
            if(((int) curChar) == ((int) newLine))
            {
                reset = true;
            }
            else if(Character.isWhitespace(curChar))
            {
                lastWSPos = strPos;
            }
            else if(curWidth > width && lastWSPos >= 0)
            {
                editable.replace(lastWSPos, lastWSPos + 1, newLineStr);
                insertCount++;
                strPos = lastWSPos;
                lastWSPos = -1;
                reset = true;
            }
    
            if(reset)
            {
                curWidth = 0.0f;
                reset = false;
            }
    
            strPos++;
        }
    
        if(insertCount != 0)
        {
             tv.setText(editable);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to extend the SPGridView control in a webpart such that
Is there any way to see what a control will look like in the
Is there a way to control the output filename from xsd.exe? My specific issue
Is there a way to control the amount that the browser is scrolled in
Is there a way to control the optimization level of the java compiler that
Is there a way to control the order that XmlSerializer generates the namespace and
Is there any way to control the progress bar that is shown on the
Is there any way to control the sorting that occurs when I run a,
Is there a way to control where Resharper puts its backing fields? currently it
Is there a way to control how .Net sets the Name and ID property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.