9

I'm looking for a way to when I change a screen size it will proportionally resize the text. Currently I tried Auto Scale TextView Text to Fit within Bounds but it doesn't seems to be working. but I'm not sure I do it the right way or not. I'm now currently calling by

AutoResizeTextView test = new AutoResizeTextView(this);
test=(AutoResizeTextView)findViewById(R.id.test456);
test.resizeText(); // I call this to resize.. am I right?

XML

<com.mypackage.AutoResizeTextView
android:id="@+id/test456"
android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"  />
Community
  • 1
  • 1
I Yeu C
  • 636
  • 2
  • 7
  • 13

3 Answers3

37

You don't need to call AutoResizeTextView test, you can say TextView since the class extends TextView. I don't see why you'd need to call resizeText() either.

Either way, here's a custom class I like to use to auto re-size text.

public class AutoFitTextView extends TextView {

public AutoFitTextView(Context context) {
    super(context);
    init();
}

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

private void init() {

    maxTextSize = this.getTextSize();
    if (maxTextSize < 35) {
        maxTextSize = 30;
    }
    minTextSize = 20;
}

private void refitText(String text, int textWidth) {
    if (textWidth > 0) {
        int availableWidth = textWidth - this.getPaddingLeft()
                - this.getPaddingRight();
        float trySize = maxTextSize;

        this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
        while ((trySize > minTextSize)
                && (this.getPaint().measureText(text) > availableWidth)) {
            trySize -= 1;
            if (trySize <= minTextSize) {
                trySize = minTextSize;
                break;
            }
            this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
        }
        this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize);
    }
}

@Override
protected void onTextChanged(final CharSequence text, final int start,
        final int before, final int after) {
    refitText(text.toString(), this.getWidth());
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != oldw) {
        refitText(this.getText().toString(), w);
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    refitText(this.getText().toString(), parentWidth);
}

public float getMinTextSize() {
    return minTextSize;
}

public void setMinTextSize(int minTextSize) {
    this.minTextSize = minTextSize;
}

public float getMaxTextSize() {
    return maxTextSize;
}

public void setMaxTextSize(int minTextSize) {
    this.maxTextSize = minTextSize;
}

private float minTextSize;
private float maxTextSize;

}
adneal
  • 30,484
  • 10
  • 122
  • 151
  • i'm calling via another class so i need to call tt am i right? Is it that all i need is to go layout xml n use the custom class as header? – I Yeu C Mar 15 '12 at 05:29
  • It seems like you got it, but to be clear; you add the custom class in your XML in place of `TextView`, then in your code you reference it like a normal `TextView`. – adneal Mar 15 '12 at 06:55
  • so that means I call TextView tv = new TextView(this); rather than AutoFitTextView tv = new AutoFitTextView(this); .. Am I right to say that? – I Yeu C Mar 15 '12 at 09:22
  • How to use it with the textView ?? – Noman Dec 12 '12 at 07:03
  • @Noman, just replace at the layout.xml "TextView" to "yourpackage.AutoFitTextView". It works perfect for me!!! Very clear and effective code :D – xarlymg89 Jan 09 '13 at 18:41
  • awesome! saved me alot of time! don't forget to add some padding left & right, otherwise the text might still be cut off at the right end! – dy_ Jul 08 '13 at 16:27
  • Causes ANR in some situations. – tomrozb Sep 26 '13 at 13:54
4

Good news! Google introduced Autosizing TextView support in Android O. It will be also included in support librararies.

https://developer.android.com/preview/features/autosizing-textview.html

Tom Wayne
  • 1,288
  • 1
  • 12
  • 31
2

I use this, proportionally at the width of the screen.

DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        width_screen=displaymetrics.widthPixels;
Mytext.setTextSize(TypedValue.COMPLEX_UNIT_PX, (width_screen/CONST));

the CONST is a number I use for to scale the font, in the dimension I want. It works for my needs.

Boris Karloff
  • 1,190
  • 12
  • 20