Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android accessibility with TalkBack: Adding a 'role' to a custom view object is not reading as a button or a link

I'm working to make a HubDisclaimerView accessible with TalkBack and the role of "button" or "link" isn't being applied.

I've set a content description to announce what the text is, but the view does not inherit from Button or ImageView. It does have an onClickListener and isClickable=true. Yet no "Double Click to ..." or "Button" or "Link" accessibility features show in TalkBack mode or in the Local Context menu.

I'm wondering if there's a fix to making a custom view a Link for accessibility purposes? The AccessibilityEvent API seems to not be the right direction, since I'm hoping to add this to a row in a list which is acting as a Term & Conditions link.

like image 775
Emma Twerk Avatar asked Sep 02 '25 10:09

Emma Twerk


1 Answers

You can add information inside onInitializeAccessibilityNodeInfo() to tell accessibility services like Talkback that the view is clickable. I don't have your code, but wrote a trivial custom view to demonstrate this:

class MyTextView : TextView {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)

    // ... other override methods

    override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo?) {
        super.onInitializeAccessibilityNodeInfo(info)
        // With `null` as the second argument to `AccessibilityAction`, Talkback announces
        // "double tap to activate". If a custom string is provided, 
        // Talkback announces "double tap to <custom string>".
        val customClick = AccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, "foo")
        info?.addAction(customClick)
    }
}

There isn't a real reason to add a role, since Talkback will make the "double tap to X" announcements with this small change. If you absolutely wanted to set the role, you could do so as follows (but again, I'm not sure if anything is gained by doing so):

override fun getAccessibilityClassName(): CharSequence {
    return Button::class.java.name
}

This makes Talkback add "Button" to the announcement, but adding a role in this manner is a hack. There's no guarantee that Talkback will continue to announce the view as a button just because you set the accessibilityClassName. Stick with adding AccessibilityNodeInfo.ACTION_CLICK.

like image 169
Shailen Tuli Avatar answered Sep 03 '25 23:09

Shailen Tuli