Autosizing EditText Autosizing EditText android android

Autosizing EditText


I was stuck as you, how EditText is child of TextView but don't support autosize ¿?¿?

I have achieve this with some kind of hack.First I saw the TextView code to copy and implement as extension (in Kotlin) on EditTextView, but.. there is a bulk of methods, so endly I discard that option.

What I have do, it's to use a TextView invisible (yes I know is a complete hack, am not very happy with that but Google should be ashamed about this)

This is my xmls

    <TextView android:id="@+id/invisibleTextView"    android:layout_height="0dp"    android:layout_width="match_parent"    android:focusable="false"    app:autoSizeTextType="uniform"    app:autoSizeMinTextSize="@dimen/text_min"    app:autoSizeMaxTextSize="@dimen/text_max"    app:autoSizeStepGranularity="@dimen/text_step"    android:textAlignment="center"    app:layout_constraintLeft_toLeftOf="@id/main"    app:layout_constraintRight_toRightOf="@id/main"    app:layout_constraintTop_toBottomOf="@id/textCount"    app:layout_constraintBottom_toBottomOf="@id/main"    android:visibility="invisible"    tool:text="This is a Resizable Textview" /><EditText android:id="@+id/resizableEditText"    android:layout_height="0dp"    android:layout_width="match_parent"    android:textAlignment="center"    app:layout_constraintLeft_toLeftOf="@id/main"    app:layout_constraintRight_toRightOf="@id/main"    app:layout_constraintTop_toBottomOf="@id/textCount"    app:layout_constraintBottom_toBottomOf="@id/main"    android:maxLength="@integer/max_text_length"    tool:text="This is a Resizable EditTextView" />

Note: It's important both view have the same width/height

Then on my code I use the autocalculations from this textview to use on my EditTextView.

private fun setupAutoresize() {    invisibleTextView.setText("a", TextView.BufferType.EDITABLE) //calculate the right size for one character    resizableEditText.textSize = autosizeText(invisibleTextView.textSize)    resizableEditText.setHint(R.string.text_hint)    resizableEditText.addTextChangedListener(object : TextWatcher {        override fun afterTextChanged(editable: Editable?) {            resizableEditText.textSize = autosizeText(invisibleTextView.textSize)        }        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {            textCount.text = currentCharacters.toString()            val text = if (s?.isEmpty() ?: true) getString(R.string.text_hint) else s.toString()            invisibleTextView.setText(text, TextView.BufferType.EDITABLE)        }    })}private fun autosizeText(size: Float): Float {    return size / (resources.displayMetrics.density + MARGIN_FACTOR /*0.2f*/)}

As note, for change the size of hint i use this Android EditText Hint Size.

I know it's a hard workaround, but at least we are sure this will continue working even when resizable change on future versions, while a propetary or abandoned github lib will fail.

I hope some day, google hear us and implement on childs this wonderfull feature, and we could avoid all this stuff

Hope this helps


You can use a RelativeLayout to hide a TextView behind an EditText, both of which will have equal height and width.The TextView will have android:autoSizeTextType="uniform"By using setOnTextChangedListener on the EditText, you can set the text of the TextView to whatever is in the EditText. Then the text size of the TextView will be adjusted automatically. Then you have to set the text size of the EditText as same as that of the TextView. Here is the layout:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" tools: context=".MainActivity"><TextViewandroid:id="@+id/test"android:layout_width="match_parent" android:layout_height="match_parent" android:padding="0dp"android:layout_margin="0dp"android: autoSizeTextType="uniform"android: autosizeMaxTextSize="500sp" android: autosizestepGranularity="1sp"/><EditTextandroid:id="@+id/edit"android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="0dp"/>android:padding="0dp"</RelativeLayout>

And the code:

public void code(){edit.setMovement Method (null);edit.addTextChangedListener(new Textwatcher() {@Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) { test.setText (edit.getText().tostring()); edit.setTextSize(pixel2dip(test.getTextSize()));}public static float pixel2dip(float a) {int b = (int) (a);int c = (int) (b / Resources.getSystem().getDisplayMetrics ().scaledDensity); return (float) (c);});}