Skip to main content
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.

On Android, you can use the setAccessibilityDelegate method of ViewCompat to get a reference to AccessibilityNodeInfoCompat. This object contains many useful accessibility related methods.

You can set a role using the setRoleDescription method. However, we recommend using the setClassName method over setRoleDescription to support multilingual roles. For example, set Button::class.java.name if an element behaves like a button. The role will be set to Button in English, and to its respective translation in other languages.

You can indicate a heading by using the setHeading method. ViewCompat also contains a convenience method: setAccessibilityHeading.

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)

Feedback?

Let us know!