Accessibility state on Android
An accessibility state helps users of assistive technologies to understand the state of elements on the screen. The state selected
for example, indicates that an element has been selected. The screen reader announces the state of elements as it reads the screen. It is important to assign correct states of 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 an accessibility state by using the setStateDescription
method. A convenience method is available in ViewCompat
, which is also named setStateDescription
.
You can also use the setChecked
method to indicate a checked state and the setSelected
method to indicate a selected state.
ViewCompat.setStateDescription(view, "Expanded")
ViewCompat.setAccessibilityDelegate(
view,
object : AccessibilityDelegateCompat() {
override fun onInitializeAccessibilityNodeInfo(
host: View,
info: AccessibilityNodeInfoCompat
) {
super.onInitializeAccessibilityNodeInfo(host, info)
// Custom state
info.stateDescription = "Expanded"
// Checked
info.isChecked = true
// Selected
info.isSelected = true
}
}
)