Manage font using AutoSizing

59 Views Asked by At

I was wondering if there is a way that I can make my string font size auto adjustable in android meaning if the word size is too long for space, the font becomes smaller by itself. As it is shown in the picture attached my buttons are made using Java and not XML so the function needs to be in Java. Also, the minimum supported Android version is API 21 so the new AutoSizing feature in Android won't work.

1

There are 1 best solutions below

0
On

Something similar to this should work.

if(view.getText().length()>18){
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.smallTextsize));
    }else
    if(view.getText().length()>10){
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.mediumTextsize));
    }else {
        view.setTextSize(TypedValue.COMPLEX_UNIT_PX,getResources().getDimension(R.dimen.largeTextsize));
    }