0

I want to prevent Talkback from announcing class name for Button view. To do so I wrote following logic but it doesn't work

fun setAccessibilityDelegate(view: Button?) {
    view?.setAccessibilityDelegate(object : View.AccessibilityDelegate() {
        override fun onInitializeAccessibilityNodeInfo(host: View?, info: AccessibilityNodeInfo?) {
            super.onInitializeAccessibilityNodeInfo(host, info)
            info?.contentDescription = getContentDesStartWatching()
            info?.className = ""
        }
    })
}

Actual description: "start watching button, Button double tap to activate"

Expected description: "start watching button, double tap to activate"

Note: cannot change the content description to "start watching" as it is getting retrieved from backend

musica
  • 1,373
  • 3
  • 15
  • 34
  • See https://stackoverflow.com/questions/37843919/android-talkback-announces-class-type-at-end-of-content-description/51365163 But you should probably leave it the way it is. Perhaps you think it sounds weird to have the class name announced, but those who use these features may be expecting views to be announced in that way. – Michael Feb 04 '20 at 12:36
  • @Michael I am already class name from the backend which I am adding as content description for the Button and hence Button is getting announced twice – musica Feb 04 '20 at 13:06
  • what is the contentDescription? – Ryan M Feb 04 '20 at 15:24
  • why not just add something that strips ' button' from the backend text on the front end if you can't change the backend. This would be the correct description at that point. Don't do Android but I am sure a quick `string.replace(" button", "")` (using the equivalent method) would be simple enough. – GrahamTheDev Feb 04 '20 at 22:17

1 Answers1

0

This worked for me, used AccessibilityDelegateCompat instead of AccessibilityDelegate

Kotlin code:

fun setAccessibilityDelegate(view: View) {
    ViewCompat.setAccessibilityDelegate(view, object: AccessibilityDelegateCompat() {
        override fun onInitializeAccessibilityNodeInfo(host:View,
                                                       info: AccessibilityNodeInfoCompat) {
            super.onInitializeAccessibilityNodeInfo(host, info)
            info.className = null
            info.contentDescription = getContentDesStartWatching()
        }
    })
}
musica
  • 1,373
  • 3
  • 15
  • 34