How to correctly specify artithmetic expression in TextView for recognition by TalkBack

Viewed 20

For a fitness app, I'm looking to display a message on a smartwatch emulator which can be read by the TalkBack facility and (when spoken) is understandable by the user when TalkBack highlights the TextView. The + sign has several different meanings, e.g. plus, add, positive, increase, etc. In this case I want the word 'plus' to be spoken by TalkBack. Is this something that Java can provide? This also needs to be something understandable by different languages when strings are localised.

Expected result

Text on display: Number of steps: 4↑ (2↑ + 2↑)

Talkback should say: "4 up (2 up + 2 up)"

Current code

    val imageGetter = Html.ImageGetter { name ->
        val resId = if (name == "arrow") { R.drawable.ic_arrow_up }
        else { throw IllegalArgumentException("what the heck is $arrow") }

        ResourcesCompat.getDrawable(resources, resId, requireActivity().theme)?.apply {
            setBounds(0, 0, intrinsicWidth, intrinsicHeight)
        }
    }

    val txtNumberOfSteps = getString(R.string.number_of_steps)
    val txt4 = String.format("%d", 4) 
    val txt2 = String.format("%d", 2) 
    val htmlTxtSteps = "$txtNumberOfSteps $txt4 <img src=\"arrow\"/> ($txt2 <img src=\"arrow\"/> + $txt2" <img src=\"arrow\"/>)"
    val txtStepsClimbed = Html.fromHtml(htmlTxtSteps, Html.FROM_HTML_MODE_COMPACT, imageGetter, null)

    myTv.setText = txtStepsClimbed

strings.xml

<string name="number_of_steps">Number of steps: </string>
1 Answers

Try updating the contentDescription of the TextView every time the values change.

For example, for

Text on display: Number of steps: 4↑ (2↑ + 2↑)

you could call

myTv.contentDescription = "Four up times two up plus two up"

I might've misrepresented the parenthesis but you get the gist. Simply, set a String that says exactly what you want the TalkBack feature to read back to you.

Hoping this helps!

Related