'Get position of child view and show annotation with Accessibility Android
I am trying to achieve the text detection mechanism similar to how the app Hi Dicitonary quick translate function works https://i.stack.imgur.com/xPjbh.gif
( I make a screenshot video below https://drive.google.com/file/d/1CZ67PK7_12iC4nrIk-GB5bDBNnPn8J-e/view). The floating button is able to get the text of the "nearest surface" where the floating button ends its dragging gesture. I tested and it worked with both regular edittext and textview, and also text from a browser like chrome.I am thinking of an accessibility service that listens for events from other apps with onAccessibilityEvent, and will draw annotation views to the top most windows or top most activity once it correctly capture the text and its position in the parent view. I tried the suggested answer at Android onAccessibilityEvent get X and Y position of TextView as in Rect rect = new Rect(); textNodeInfo.getBoundsInScreen(rect);but when I drag the floating ball to an textview in the middle left of the screen and log to the console, the rect ordinate resulted from the getBoundsInScreen strangely varies to negative value such as Rect(0, 626 - -4144, 672) ( the full log is like onAccessibilityEvent: type = [ 2048 ], class = [ android.view.ViewGroup ], package = [ com.talpa.translate ], time = [ 97019680 ], text = [ [] ] I/package name: The TextView Node: Rect(0, 626 - -4144, 672)) .Coming from a iOs background when thinking of the position of a child view in its parent, I suppose the value of width and height calculated as width = right - left = (-4144 - 0) = -4414 ; height = bottom - top = 672 - 626 = 46 should always be positive. Why do I get a negative result here? Is the rect returned the position of a child view( node view) in it "nearest" descendant parents? How do I convert the position of that child view relative to the "furthest" ancestor - the top most window?
What is the best approach to achieve the way the Hi Dictionary app detect its target "focused" text and draw annotation view? I could now just think of the above approach, for this way I think I have to loop through all child view and store its position, then compare to the position of the floating button once it dragging gesture ends and find out the child view that is "closest" to the position of the floating button. If the view hiearchy is multiple-nested, what position should I consider?
Your help is much appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
