Hiroga Katageri
Hiroga Katageri

Reputation: 1426

Given text can not be applied to TextView when TextView parameters are changed

So I've made an extension to implement Precomputed Text in a TextView.

I've been getting errors though ever since I've migrated to Material Components Theme.

Here's the code below.

I've also tried migrating all TextViews, and AppCompatTextViews to MaterialTextView. Yet the issue is still occurring.

fun AppCompatTextView.setTextFuture(string: String?) {
    val precomputedText =
        PrecomputedTextCompat.getTextFuture(
            string ?: "",
            TextViewCompat.getTextMetricsParams(this),
            null
        )

    textMetricsParamsCompat = precomputedText.get().params
    setTextFuture(precomputedText)
    text = string

}

fun AppCompatTextView.setTextFuture(charSequence: CharSequence?) {
    val precomputedText =
        PrecomputedTextCompat.getTextFuture(
            charSequence ?: "",
            TextViewCompat.getTextMetricsParams(this),
            null
        )

    textMetricsParamsCompat = precomputedText.get().params
    setTextFuture(precomputedText)
    text = charSequence
}

fun AppCompatTextView.setTextFuture(stringResId: Int) {
    val string = context.getString(stringResId)

    val precomputedText =
        PrecomputedTextCompat.getTextFuture(
            string,
            TextViewCompat.getTextMetricsParams(this),
            null
        )

    textMetricsParamsCompat = precomputedText.get().params
    setTextFuture(precomputedText)
    text = string
}

The errors have been the following.

Fatal Exception: java.lang.IllegalArgumentException
Given text can not be applied to TextView.
androidx.core.widget.TextViewCompat.setPrecomputedText (TextViewCompat.java:891)
androidx.appcompat.widget.AppCompatTextView.onMeasure (AppCompatTextView.java:550)
android.view.View.measure (View.java:24953)
Fatal Exception: java.lang.IllegalArgumentException
PrecomputedText's Parameters don't match the parameters of this TextView.Consider using setTextMetricsParams(precomputedText.getParams()) to override the settings of this TextView: PrecomputedText: {textSize=49.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_GB], typeface=android.graphics.Typeface@5e608edd, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@eb941bf, breakStrategy=1, hyphenationFrequency=0}TextView: {textSize=49.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_GB], typeface=android.graphics.Typeface@5e608edd, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@eb941bf, breakStrategy=1, hyphenationFrequency=0}
android.widget.TextView.setText (TextView.java:6724)
Fatal Exception: java.lang.IllegalArgumentException
PrecomputedText's Parameters don't match the parameters of this TextView.Consider using setTextMetricsParams(precomputedText.getParams()) to override the settings of this TextView: PrecomputedText: {textSize=70.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_CA], typeface=android.graphics.Typeface@ab2ab1bb, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@34fe469, breakStrategy=1, hyphenationFrequency=0}TextView: {textSize=70.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_CA], typeface=android.graphics.Typeface@ab2ab1bb, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@34fe469, breakStrategy=1, hyphenationFrequency=0}
android.widget.TextView.setText (TextView.java:6731)

Some of my TextViews have these properties

android:ellipSize="marquee"
android:singleLine="true"

isSelected = true

Upvotes: 3

Views: 1451

Answers (1)

Amani
Amani

Reputation: 3979

all TextView layout properties must be set before creating the Params object. If they are changed during the precomputation, this can cause a IllegalArgumentException when the precomputed value is consumed during measure, and doesn't reflect the TextView's current state.

https://developer.android.com/reference/androidx/core/text/PrecomputedTextCompat

so its important when you call this method: setTextFuture

Upvotes: 4

Related Questions