Skip to content

Commit

Permalink
Merge pull request #76 from douglaskazumi/patch-1
Browse files Browse the repository at this point in the history
Add remarks about EditText role
  • Loading branch information
JJdeGroot authored Dec 20, 2024
2 parents d7ccbd8 + 46841b1 commit 9659866
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/data/code-samples/en/accessibility-role/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,43 @@ ViewCompat.setAccessibilityDelegate(
// Convenience method
ViewCompat.setAccessibilityHeading(view, true)
```

## EditText considerations

Hidden in [TalkBack's source code](https://github.com/google/talkback/blob/master/utils/src/main/java/com/google/android/accessibility/utils/Role.java#L303-L311) there's a special case for `EditText`:

```kotlin
if (ClassLoadingCache.checkInstanceOf(className, android.widget.EditText.class)) {
if (node.isEnabled() && !node.isEditable()) {
// Developers may want to provide extra information
// when an EditText is enabled but not editable.
return ROLE_NONE;
} else {
return ROLE_EDIT_TEXT;
}
}
```

Meaning that in order for the correct role to be announced, the `EditText` needs to be editable, otherwise `ROLE_NONE` will be set to the component.

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

// EditText
info.className = EditText::class.java.name
info.isEditable = true
}
}
)

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

0 comments on commit 9659866

Please sign in to comment.