Search code examples
androidkotlinsearchaccessibilitytalkback

Android accessibility: how to change the search bar readed string


as I wrote in the title, the Android Talkback actually reads the hint text of my search bars, but I need to change that behavior to read a custom string (different from the hint). I tried to set the contentDescription but it didn't work (the talkback still reads the hint text).

Do you have any advice?


Solution

  • You can set an AccessibilityDelegate on a view to override things like how it describes itself in general:

    ViewCompat.setAccessibilityDelegate(yourView, object : AccessibilityDelegateCompat(){
        override fun onInitializeAccessibilityNodeInfo(host: View?, info: AccessibilityNodeInfoCompat?) {
            super.onInitializeAccessibilityNodeInfo(host, info)
            info?.let { it.text = "whatever" }
        }
    })
    

    or you can override onPopulateAccessibilityEvent (which handles triggered events like AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) and use event.getText(), which gives you a List<CharSequence> you can add your description to. It really depends on what you're doing and how dynamic it needs to be.

    I haven't found a good overview of all this stuff unfortunately, otherwise I'd link you to it. I think the delegates are what you need to look into though