A
A
Artem2012-09-08 23:55:34
Android
Artem, 2012-09-08 23:55:34

Is text autoscaling possible?

There is a markup, in which the page is divided into two parts - the heading and the body. The heading contains a line of text and an image on the right. The header height can be changed. Accordingly, the text and image inside the heading will have to be scaled to the new height. The picture scales normally, but the text does not want to change its size ... Is it possible how to implement this inside the markup?
The code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="UseCompoundDrawables" >

        <TextView
            android:id="@+id/title_text"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/app_name" />

        <ImageView
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/edit"
            android:src="@drawable/widget_edit" />
    </LinearLayout>

    <TextView
        android:id="@+id/content_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AChep, 2012-09-10
@bartwell

Yes it is possible

private float getSizeFitText(String text, int textWidth) {
    Paint mTestPaint = new Paint();

    if (textWidth <= 0)
      return 0;
    int targetWidth = textWidth;
    float hi = 100;
    float lo = 2;
    final float threshold = 0.5f; // How close we have to be

    while ((hi - lo) > threshold) {
      float size = (hi + lo) / 2;
      mTestPaint.setTextSize(size);
      if (mTestPaint.measureText(text) >= targetWidth)
        hi = size; // too big
      else
        lo = size; // too small
    }
    // Use lo so that we undershoot rather than overshoot
    return lo - 10;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question