Calculate the size of the text based on the height of the TextView

191 views Asked by At

I have a TextView within a HorizontalScrollView, as shown below. Currently, I am using a fixed text size. Now, I would like to calculate the maximum text size that I can use without the text size exceeding the height of the TextView. Note that the width of the text is not an issue, since the TextView is a child of the HorizontalScrollView, so the text becomes scrollable as soon it extends the allowable width.

<HorizontalScrollView
    android:id="@+id/horizontal_scroll_view_equation"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_gravity="right">

    <LinearLayout
        android:layout_gravity="right"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:layout_gravity="right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</HorizontalScrollView>

I've been looking into posts on StackOverflow and another forums, but I could only find posts that consider both the width and height.

Is there a simple solution to calculate the maximum text size based on the height of the TextView?

1

There are 1 answers

0
t.r.hogenkamp On

I actually found the answer myself. The solution is as follows. Firstly, I made a custom TextView, as shown below.

import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;


public class AutoSizeTextView extends AppCompatTextView {

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

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

    public AutoSizeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        this.getPaint().setTextSize(h * 0.75f);
        invalidate();
    }
}

And then I used this TextView as child in my HorizontalScrollView, as shown below.

<HorizontalScrollView
    android:id="@+id/horizontal_scroll_view_equation"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_gravity="right">

    <LinearLayout
        android:layout_gravity="right"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:orientation="vertical">

        <com.yourpackage.AutoSizeTextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:layout_gravity="right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</HorizontalScrollView>