Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selected text from AccessibilityNodeInfo

I have a accessibility service running for event type "typeViewTextSelectionChanged". I am able to catch this event trigger whenever user selects any text, but how do I get the selected text content from AccessibilityNodeInfo or AccessibilityEvent objects

like image 584
Vishwesh Shetty Avatar asked Feb 28 '17 08:02

Vishwesh Shetty


1 Answers

The following goes inside,

onAccessibilityEvent(AccessibilityEvent event){}

And then,

//Get the source
AccessibilityNodeInfo source = event.getSource();

//Grab the parent of the view that fired the event.
AccessibilityNodeInfo rowNode = getListItemNodeInfo(source);

//Using this parent, get references to child node, the selected text
AccessibilityNodeInfo textNode = rowNode.getChild(0);

//Get the text values
String text = textNode.getText();

OR

Alternatively in your case, the following should work just fine. Since, it's a "typeViewTextSelectionChanged" event, it's obviously from an EditText.

String text=event.getText();

For more info, have a look here and here

like image 80
Darshan Miskin Avatar answered Nov 18 '22 03:11

Darshan Miskin