SagePawan
SagePawan

Reputation: 364

Implement autoSizeTextType in android Edittext

Android Oreo buildToolsVersion provides a simplified way to autoresize textsize in AppCompatTextView as follows

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:maxWidth="300dp"
    android:background="@android:color/holo_green_light"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="5sp"
    app:autoSizeMaxTextSize="50sp"
    app:autoSizeStepGranularity="4sp"
    />

Can a similar implementation be applied to AppCompatEditText since it is basically an extension of TextView? Simply applying autoSizeTextType to AppCompatEditText doesn't seem to work. Is there a way to make this work out?

Upvotes: 6

Views: 3775

Answers (3)

nglauber
nglauber

Reputation: 23864

I had an specific case which was a single line EditText, so I'm posting my solution just in case to be helpful to someone...

private val initialTextSize: Float by lazy {
    resources.getDimensionPixelSize(R.dimen.default_text_size).toFloat()
}

private fun updateTextSize(s: CharSequence) {
    // Auto resizing text
    val editWidth = myEditText.width
    if (editWidth > 0) { // when the screen is opened, the width is zero
        var textWidth = myEditText.paint.measureText(s, 0, s.length)
        if (textWidth > editWidth) {
            var fontSize = initialTextSize
            while (textWidth > editWidth && fontSize > 12) { // minFontSize=12
                fontSize -= 1
                myEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
                textWidth = myEditText.paint.measureText(s, 0, s.length)
            }
        // As long the text grows, the font size decreases, 
        // so here I'm increasing it to set the correct text s
        } else {
            var fontSize = myEditText.textSize
            while (textWidth <= editWidth && fontSize < initialTextSize) {
                fontSize = min(fontSize + 1, initialTextSize)
                myEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
                textWidth = myEditText.paint.measureText(s, 0, s.length)
            }
        }
    }
}

Then you must call this function from a TextWatcher attached to the EditText.

private fun createTextWatcher() = object : SimpleTextWatcher() {
    override fun beforeTextChanged(s: CharSequence?, 
        start: Int, count: Int, after: Int
    ) {
        super.beforeTextChanged(s, start, count, after)
        s?.let { updateTextSize(it) }
    }

    override fun onTextChanged(
        s: CharSequence?,
        start: Int,
        before: Int,
        count: Int
    ) {
        // Do nothing
    }

    override fun afterTextChanged(s: Editable?) {
        super.afterTextChanged(s)
        s?.let { updateTextSize(it) }
    }
}

Upvotes: 3

CrandellWS
CrandellWS

Reputation: 2804

My suggestion would be use: --> https://github.com/AndroidDeveloperLB/AutoFitTextView correction https://github.com/ViksaaSkool/AutoFitEditText

See this thread for more info: --> Auto-fit TextView for Android

General Article on mention library by author: --> https://viksaaskool.wordpress.com/2014/11/16/using-auto-resize-to-fit-edittext-in-android/

Upvotes: 0

Siyamed
Siyamed

Reputation: 505

No you cannot. Please see here; it is disabled for all AppCompatEditText since it is not supported.

Upvotes: 4

Related Questions