Reputation: 725
I'm using AccessibilityService
to crawl through fields in other apps or browser.
For get the text of fields, I have this below snippet code:
AccessibilityNodeInfo node;
text = node.getText().toString();
But this, in some fields, specially in browser, give me the hint text of the field if it's empty, and I can't excide text value from hint value.
I know in Android O, we will have the AccessibilityNodeInfo.getHintText()
for this, it's cool, but I need to use my app in previous versions of Android.
Does anybody have any solution for my condition?!
***UPDATE I've been tried this below solution to achieve my goal:
/** Ummm, didn't work :( */
public static boolean isTextViewWithHint(AccessibilityNodeInfo node)
{
boolean isHint;
String text, hint;
text = node.getText().toString();
Log.d(TAG, "text = " + text);
Bundle arguments = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "");
node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
Log.d(TAG, "node.refresh() = " + node.refresh());
hint = node.getText().toString();
Log.d(TAG, "hint = " + hint);
if (text != null && hint != null && text.equals(hint))
{
// current value of node is a Hint!
Log.d(TAG, text + " is a Hint.");
isHint = true;
}
else
{
Log.d(TAG, text + " is real value.");
isHint = false;
}
Bundle arguments2 = new Bundle();
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, text);
node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments2);
return isHint;
}
But it doesn't work, because when I clear the field, the hint = node.getText().toString();
still give me the same value that the text = node.getText().toString();
gived me before! I don't know why?!
If the hint = node.getText().toString();
line (after I clear the field) gived me the hint text of the field, or empty value, my problem would be solved.
Does anybody see any issue with my above snippet code?!
Upvotes: 0
Views: 1682
Reputation: 18870
You can't do this. It is completely unavailable to you prior to Android O. Your options:
A: Use the media projection API and OCR to actually read the hint text in the EditText field.
B: give up.
Answer to Edits:
I understand your frustration. The Android Accessibility APIs are pretty terrible. However, you literally cannot do what you are trying to do. Not within an accessibility service. This is absolutely a platform limitation.
Prior to Android O the following is true and unchangeable:
And EditText's accessibilityNodeInfo "Text" property will be:
The Hint Text, when the text entered into the EditText field is null.
The entered text when it is not.
I see in your hack solution attempting to override the characters in the text field. Given the above, you could attempt setting the text to (null) instead of ("").
Unfortunately, if this does not work, there are no other properties you can use to get this information (< Android O). Sometimes developers use "contentDescription" to store similar information, but even this isn't "ideal" as TalkBack chokes on this scenario pretty hard.
The problem is, developers should not be relying on "Hint" text for critical information. They should be associating their EditText controls with visible labels.
Upvotes: 2