androidstringtextviewappendautosize

Autosizing not working when appending to TextView Android


So I'm making a simple calculator app and I need to make the numbers inside of the textview resize automatically when the numbers reach the edges of it. Now, if I'm using TextView.setText(TextView.getText()+"5") it does autosize no problem, but for simplicity sake I like to use TextView.append("5") . However, when using the append function the TextView resize seems to not update and doesn't size the text at all.

I've tried using both pre API level 26 resizing and post level 26 resizing. Example of pre API level 26 resizing:

        android:layout_height="60dp"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="12sp"
        app:autoSizeMaxTextSize="100sp"
        app:autoSizeStepGranularity="2sp"

Does anyone have any idea how I could do this? Or am I stuck using TextView.setText(TextView.getText()+"5")? Thanks in advance!


Solution

  • Unfortunately, I think that you will have to go back to TextView.setText(TextView.getText()+"5"). Here is my reasoning:

    TextView.append() is defined as:

    Convenience method to append the specified text to the TextView's display buffer, upgrading it to TextView.BufferType.EDITABLE if it was not already editable.

    Changing the buffer type to TextView.BufferType.EDITABLE takes the TextView into the realm of EditText (EditText extends TextView) and EditText does not support auto resizing:

    From the documentation for EditText:

    This widget does not support auto-sizing text.