Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android accessibility service - Clearing an EditText

Tags:

android

I am trying to insert text inside an EditText using android's AccessibilityService. Currently, I am doing something like:

textnode.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
ClipData clip = ClipData.newPlainText("label", "DATA" );
clipboard.setPrimaryClip(clip);
textnode.performAction(AccessibilityNodeInfo.ACTION_PASTE);

My problem is that if there is some text present inside the EditText, the new text is appended to it, whereas I want it replaced. I can't find any way to clear the text in the EditText from inside the accessibility service.

I feel as if I am missing something since this should be an easy task. Please tell me how to accomplish this.

like image 812
rahules Avatar asked Dec 01 '25 07:12

rahules


2 Answers

I had to use some kind of a hack. Basically, it selects the text in the EditText and pastes the new text over it. In my case, I had to replace a single word, so I just had to select the first word Here's the code:

Bundle arguments = new Bundle();
arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                    AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD);
arguments.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
                    true);
textnode.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
                    arguments);
ClipData clip = ClipData.newPlainText("label", "NEWDATA");
clipboard.setPrimaryClip(clip);
usernode.performAction(AccessibilityNodeInfo.ACTION_PASTE);
like image 129
rahules Avatar answered Dec 03 '25 20:12

rahules


use this,hope can help you

Bundle arguments = new Bundle();   

arguments.putCharSequence(
AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
"your info");

nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
like image 28
yongzheng Avatar answered Dec 03 '25 21:12

yongzheng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!