Android accessibility: how to change text read aloud to view EditText

By default, accessibility services will read the following for the EditText view.

  • If EditText has value entered = it will read that value
  • If no values ​​are entered = it will read the "prompt"

I want him to read something completely different in both cases.

My xml snippet

<EditText android:id="@+id/my_edit_text" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:editable="false" android:focusable="true" android:hint="my hint text"/> 

I need to support API 14 onwards.

I don't want to worry about the EditText extension for this single case, so I am using am AccessibilityDelegate.

 mEditTextView.setAccessibilityDelegate(accessibilityDelegate); 

From the documentation, I understand that in my delegate, I only need to overwrite these methods in the delegate, for which I would like to change the behavior. All other methods will be implemented by default in the view.

http://developer.android.com/reference/android/view/View.AccessibilityDelegate.html http://developer.android.com/reference/android/view/View.html

The document for "onPopulateAccessibilityEvent" says: "Enables the View host to populate the availability event with its text content." The document for "dispatchPopulateAccessibilityEvent" says: "Sends an AccessibilityEvent to the View host first and then to its child words to add their text content to the event." and that the default behavior is to call "onPopulateAccessibilityEvent" for the view itself, and then "dispatchPopulateAccessibilityEvent" for all its children

http://developer.android.com/guide/topics/ui/accessibility/apps.html

This document is in the "onPopulateAccessibilityEvent" section * If your implementation of this event completely overrides the output text, preventing other parts of your layout from changing its contents, then do not call the super implementation of this method in your code. "

Therefore my delegate is next

 View.AccessibilityDelegate accessibilityDelegate = new View.AccessibilityDelegate() { @Override public void onPopulateAccessibilityEvent(View v, AccessibilityEvent event) { event.getText().add("Apples"); } }; 

Why, when I use the keyboard to go to the EditText screen or use the screen, does it still read “my hint text” and not “Apples”?

If I use the debugger, I see that before I set the event text, the text is empty, and after I set it, it’s “Apples”, but TalkBack still reads the hint.


If I overwrite "onInitializeAccessibilityNodeInfo" and sent an event with my desired text, then this desired text will be read (see the code snippet below). But this seems to me wrong, because "onInitializeAccessibilityNodeInfo" responds to the EditText event, and then just raises a new one.

 @Override public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfo info){ super.onInitializeAccessibilityNodeInfo(v, info); ... final AccessibilityEvent event = AccessibilityEvent.obtain(eventType); event.getText().add("Pears"); event.setClassName(className); event.setPackageName(packageName); ... v.getParent().requestSendAccessibilityEvent(v, event); } 
+6
source share
2 answers

This sounds very similar to the problem I encountered earlier: Android: How to remove spoken text from AccessibilityEvents when expanding SeekBar?

Have you tried (as a test) to clear the hint text in onPopulateAccessibilityEvent , and not just add the text "Apples"? I seem to remember that my empirical results did not match the Android documentation, especially for Android OS up to API 14.

+1
source

We can change the text read in EditText view by doing the following:

 View.AccessibilityDelegate accessibilityDelegate = new View.AccessibilityDelegate() { @Override public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(v, info); info.setText("some customized text") } }; 

and then set this delegate to the EditText view.

0
source

Source: https://habr.com/ru/post/974741/


All Articles