12

I'm developing an Android Accessibility Service. I got an AccessibilityNodeInfo that represents an EditText. Is possibile to edit the contained text?

I tried with mynode.setText("aaa") but i get IllegalStateException as described in official documentation http://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html

Any ideas? Thanks

user2196877
  • 121
  • 1
  • 1
  • 4
  • No, it is not possible to set the contents of an EditText from an AccessibilityService using AccessibilityNodeInfo. Accessibility nodes are only meant for reading information and performing a small set of actions (see AccessibilityNodeInfo.performAction). However, you could accomplish through an IME. – alanv May 06 '13 at 01:39
  • @alanv, how would you do that? – r2DoesInc Apr 16 '14 at 16:10
  • 3
    Actually, it looks I was wrong. As of API 18 you can use ACTION_PASTE to insert text from ClipboardManager. Combined with the text selection actions, you could effectively edit a portion of text; however, the API wasn't really intended for that and it may not work well. – alanv Apr 17 '14 at 21:44

1 Answers1

25

You can use ACTION_SET_TEXT for >= android 21. Here is the example of it:

AccessibilityNodeInfo source = event.getSource();
if (source != null & event.getClassName().equals("android.widget.EditText")) {
    Bundle arguments = new Bundle();
    arguments.putCharSequence(AccessibilityNodeInfo
            .ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "android");
    source.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
}
Simas
  • 43,548
  • 10
  • 88
  • 116
Anton Kashpor
  • 1,255
  • 2
  • 18
  • 34