Skip to content
Advertisement

TextView setContentDescription not working

I’m trying to set the contentDescription on a TextView but it doesn’t seem to be announced by TalkBack. TalkBack is only vocalizing the text content.

textView.setText("2d 9h left");
textView.setContentDescription("2days 9hours left");

The second line above has no effect. TalkBack says, “2d 9h left”.

I’ve also tried the following:

    ViewCompat.setAccessibilityDelegate(textView, new AccessibilityDelegateCompat()
    {
        @Override
        public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event)
        {
            super.onPopulateAccessibilityEvent(host, event);
            event.getText().add(host.getContentDescription());
        }
    });

but this results in TalkBack announcing “2days 9hours left 2d 9h left”, which is also incorrect.

Advertisement

Answer

The only that worked for me is AccessibilityDelegate (Kotlin):

val accessibilityDelegate: View.AccessibilityDelegate =
            object : View.AccessibilityDelegate() {
                override fun onInitializeAccessibilityNodeInfo(v: View?, info: AccessibilityNodeInfo) {
                    super.onInitializeAccessibilityNodeInfo(v, info)
                    info.text = "Custom Text"
                }
            }
myView.accessibilityDelegate = accessibilityDelegate
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement