Ga naar hoofdinhoud
Logo Appt Light

Accessibility role on Android

An accessibility role helps users of assistive technologies to understand the purpose of elements on the screen. The role button for example, indicates that an action will be performed on activation. The screen reader announces the role of elements as it reads the screen. It is important to assign correct roles to elements to avoid misunderstanding.

Op Android kun je handmatig een rol instellen via de setRoleDescription methode van AccessibilityNodeInfoCompat. Maar vaak is het beter om de setClassName methode te gebruiken om de rol van een bestaand element over te nemen. Stel bijvoorbeeld Button::class.java.name in als een element zich moet gedragen als een knop.

ViewCompat.setAccessibilityDelegate(
    element,
    object : AccessibilityDelegateCompat() {
        override fun onInitializeAccessibilityNodeInfo(
            host: View,
            info: AccessibilityNodeInfoCompat
        ) {
            super.onInitializeAccessibilityNodeInfo(host, info)

            // Custom
            info.roleDescription = "Custom role"

            // Button
            info.className = Button::class.java.name

            // Heading
            info.isHeading = true

            // Image
            info.className = ImageView::class.java.name
        }
    }
)

// Convenience method
ViewCompat.setAccessibilityHeading(view, true)
Bijdragen

Feedback?

Laat 't ons weten!