Search code examples
androidtextview

Why does TextView have end padding when multi line?


If you have a TextView with layout_width="wrap_content" and it has to wrap to a second line to contain the text, then it will size its width to use up all of the space available (respecting margins etc). But why is there padding at the end of the view? I just told it to wrap_content, so it should wrap that content! This seems like a bug, this is visible in the chat UI of the stock Messenger app. (The image is from my own app though. But that extra space is definitely not in the 9 patch.)

Any workaround?

Update: Responders/commenters missed the point. Maybe the image I uploaded was misleading because it was styled from my app. The problem occurs with any TextView, you can see by styling the background that the view bounds will no longer be tight. I uploaded a different image. Here is the XML for the TextViews in the image:

        <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:background="#dddddd"
    android:text="This doesn't wrap"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_gravity="center_horizontal"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_gravity="center_horizontal"
    android:background="#dddddd"
    android:text="This wraps and look, the bounds does not fit tight against the right edge of text"
    />

enter image description here


Solution

  • At first, when seeing your post, I thought that the problem was because standard Android TextView has some default padding defined in their base style. If one wants to remove it, one can try it something like:

    android:paddingEnd="0dp"
    

    or

    android:paddingRight="0dp"
    

    As your post has been updated, I understand that your problem does not come from padding, but from word wrapping. Indeed, when there are several lines to display, Android TextView use the whole available space in width.

    As stated in this post, there is no standard solution for this and you will need to customize your text view to fix its width after filling it.

    Overriding onMeasure method of your textView like below should work (inspired from "sky" answer) :

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        int specModeW = MeasureSpec.getMode(widthMeasureSpec);
        if (specModeW != MeasureSpec.EXACTLY) {
            Layout layout = getLayout();
            int linesCount = layout.getLineCount();
            if (linesCount > 1) {
                float textRealMaxWidth = 0;
                for (int n = 0; n < linesCount; ++n) {
                    textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n));
                }
                int w = (int) Math.ceil(textRealMaxWidth);
                if (w < getMeasuredWidth()) {
                    super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST),
                            heightMeasureSpec);
                }
            }
        }
    }