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 8981967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:28:01+00:00 2026-06-15T20:28:01+00:00

In my Android layout, I have a TextView that uses half of the available

  • 0

In my Android layout, I have a TextView that uses half of the available width of the screen. At runtime I set the text to a (long) email address. For instance:

googleandroiddeveloper@gmail.com

If the text does not fit into one line, Android inserts a line break, which is the desired behavior. However, the position of the line break is before the first character that does not fit in the line. The result can be something like this:

googleandroiddeveloper@gmai
l.com

I think, this is kind of ugly, especially in email addresses. I want the line break to appear right before the @ character:

googleandroiddeveloper
@gmail.com

Of course, i could add a \n in my strings.xml. But then the email address would use two lines in every case, even if it would fit into one line.

I already thought I had found a solution in adding a ZERO WIDTH SPACE (\u200B) to the email address.

<string name="email">googleandroiddeveloper\u200B@gmail.com</string>

But other than with standard spaces, Android does not detect the special space character as a breakable space and consequentially does not add a line break at this point.

As I am dealing with a lot of email addresses in multiple places of my application, I am searching for a solution to add a breakable and invisible space before the @ character, so Android wraps the email address if does not fit into one line.

  • 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-15T20:28:02+00:00Added an answer on June 15, 2026 at 8:28 pm

    @Luksprog’s solution is very good and solves the problem in many cases. However, I modified the class at several points, to make it even better. These are the modifications:

    • I used onSizeChanged instead of onMeasure for checking and manipulating the text, because there are problems with onMeasure when using LinearLayout with layout_weight.
    • I considered the horizontal padding of the text by using getPaddingLeft() and getPaddingRight()
    • At measuring afterAt I replaced position with position + 1, otherwise the resulting email address contains two @.

    Code:

    public class EmailTextView extends TextView {
    
        public EmailTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            // the width the text can use, that is the total width of the view minus
            // the padding
            int availableWidth = w - getPaddingLeft() - getPaddingRight();
            String text = getText().toString();
            if (text.contains("\n@")) {
                // the text already contains a line break before @
                return;
            }
            // get the position of @ in the string
            int position = -1;
            for (int i = 0; i < text.length(); i++) {
                if (text.charAt(i) == '@') {
                    position = i;
                    break;
                }
            }
            if (position > 0) {
                final Paint pnt = getPaint();
                // measure width before the @ and after the @
                String beforeAt = text.subSequence(0, position).toString();
                String afterAt = text.subSequence(position + 1, text.length())
                        .toString();
                final float beforeAtSize = pnt.measureText(beforeAt);
                final float afterAtSize = pnt.measureText(afterAt);
                final float atSize = pnt.measureText("@");
                if (beforeAtSize > availableWidth) {
                    // the text before the @ is bigger than the width
                    // so Android will break it
                    return;
                } else {
                    if ((beforeAtSize + afterAtSize + atSize) <= availableWidth) {
                    // the entire text is smaller than the available width
                        return;
                    } else {
                        // insert the line break before the @
                        setText(beforeAt + "\n@" + afterAt);
                    }
                }
            }
        }
    }
    

    Here is a screenshot of EmailTextView compared to default TextView:

    EmailTextView

    For all email addresses it works as I expected. The last address is not changed because the text before the @ is already too wide, so the system breaks it before and the thereby the email address is kind of messed up anyway, so there is no need to include another line break.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TextView in my android application that has a set width on
I have the following TextView in my XML layout file:- <TextView android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/autolink_test
I have learned that when using android:entries with a ListView , it uses android.R.layout.simple_list_item_1
I have a TextView that i inflate in my activity. When i set text
I am building an Android app that uses wifi. I have properly declared these
I have a layout that uses a scroll view. The first part of the
I have a custom ListView adapter that uses multiple layout types. One is the
I am trying to create an android layout that uses about 1/3 of the
I have a layout that uses an EditText to let users search a database
I have a simple Activity that uses a android:theme=@android:style/Theme.Dialog in the manifest. My activity

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.