Adam Varhegyi Adam Varhegyi - 2 months ago 6
Android Question

Android textview's view bounds are much larger than content, how to remove it?

My problem is, when i turn on a setting in the developer options to see view bounds, i can see that the orange "20" text wraps much larger space than it is requried. (top and bottom)

I tried to set:

android:includeFontPadding="false"
but it is just simple push up the content, leaving a big empty space bottom of the text's container.

How can i remove the extra space?

enter image description here

XML:

<LinearLayout
android:id="@+id/topDash"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_gray"
android:orientation="vertical" >

<TextView
android:id="@+id/RobotoTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sog_C"
android:textColor="@color/blue_medium"
android:textSize="20sp" />

<TextView
android:id="@+id/RobotoTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="20"
android:textColor="@color/orange"
android:textSize="100sp" />

<TextView
android:id="@+id/robotoTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="KM/H"
android:textColor="@color/gray_darker"
android:textSize="24sp" />
</LinearLayout>

mes mes
Answer

Very nice question. I'm not sure that it possible to do it only in XML and without setting any hard coded value for scrollY property, but I implemented a small custom class without hard coded values, which you can use without any modifications in other places. In such situations onMeasure method always come to help :) I use default font here. If you use different typeface, call textPaint.setTypeface() and set it before textPaint.getTextBounds statement.

public class CustomTextView extends TextView {
private int newWidth;
private int newHeight;

public CustomTextView(Context context) {
    super(context);
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if(newHeight == 0) {
        Rect textBounds = new Rect();
        Paint textPaint = new Paint();
        textPaint.setTextSize(getTextSize());
        textPaint.getTextBounds(getText().toString(), 0, getText().length(), textBounds);
        int descent = (int) textPaint.descent();
        newWidth = textBounds.width();
        newHeight = textBounds.height() - 2 * descent;
        setScrollY(descent);
    }

    setMeasuredDimension(newWidth, newHeight);
}
}

But this solution is only good for numbers, why only for numbers, because of descent, and that's why we cant remove that extra space ony inside the XML, it is a font property

descent